PHP MySQL Insert Multiple Records
Insert Multiple Records into MySQL Using MySQLi and PDO
The mysqli_multi_query() function can be used to execute multiple SQL statements.
The following example adds three new records to the "MyGuests" table:
Example (MySQLi - Object-Oriented)
connect_error ) { die ( " Connection failed: " . $conn -> connect_error ) ; } $sql = " INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com'); " ; $sql .= " INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Mary', 'Moe', 'mary@example.com'); " ; $sql .= " INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Julie', 'Dooley', 'julie@example.com') " ; if ( $conn -> multi_query ( $sql ) === TRUE ) { echo " new records inserted successfully " ; } else { echo " Error: " . $sql . "" . $conn -> error ; } $conn -> close ( ) ; ?>
![]() |
Note that each SQL statement must be separated by a semicolon. |
|---|
Example (MySQLi - Procedural)
" . mysqli_error ( $conn ) ; } mysqli_close ( $conn ) ; ?>Example (PDO)
setAttribute ( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION ) ; // begin transaction $conn -> beginTransaction ( ) ; // SQL statement $conn -> exec ( " INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com') " ) ; $conn -> exec ( " INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Mary', 'Moe', 'mary@example.com') " ) ; $conn -> exec ( " INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Julie', 'Dooley', 'julie@example.com') " ) ; // commit transaction $conn -> commit ( ) ; echo " new records inserted successfully " ; } catch ( PDOException $e ) { // if execution fails, roll back $conn -> rollback ( ) ; echo $sql . "" . $e -> getMessage ( ) ; } $conn = null ; ?>
Using Prepared Statements
The mysqli extension provides a second way for insert statements.
We can prepare statements and bind parameters.
The mysql extension can send statements or queries to the MySQL database without data. You can associate or "bind" variables to columns.
Example (MySQLi Using Prepared Statements)
connect_error ) { die ( " Connection failed: " . $conn -> connect_error ) ; } else { $sql = " INSERT INTO MyGuests(firstname, lastname, email) VALUES(?, ?, ?) " ; // initialize statement object for mysqli_stmt_prepare() $stmt = mysqli_stmt_init ( $conn ) ; // prepare statement if ( mysqli_stmt_prepare ( $stmt , $sql ) ) { // bind parameters mysqli_stmt_bind_param ( $stmt , ' sss ' , $firstname , $lastname , $email ) ; // set parameters and execute $firstname = ' John ' ; $lastname = ' Doe ' ; $email = ' john@example.com ' ; mysqli_stmt_execute ( $stmt ) ; $firstname = ' Mary ' ; $lastname = ' Moe ' ; $email = ' mary@example.com ' ; mysqli_stmt_execute ( $stmt ) ; $firstname = ' Julie ' ; $lastname = ' Dooley ' ; $email = ' julie@example.com ' ; mysqli_stmt_execute ( $stmt ) ; } } ?>We can see that the example above uses a modular approach to handle the problem. We can achieve simpler reading and management by creating code blocks.
Pay attention to parameter binding. Let's look at the code in mysqli_stmt_bind_param():
mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);
This function binds parameters to the query and passes them to the database. The second parameter is "sss". The list below shows the parameter types. The s character tells mysql that the parameter is a string.
Can be one of the following four parameters:
- i - integer
- d - double
- s - string
- b - binary blob storage object
Each parameter must specify a type to ensure data security. Type checking can reduce the risk of SQL injection vulnerabilities.
