Skip to main content

PHP Insert Multiple Data In MySQL

Screenshot
Body

If you are looking for a tutorial on How To Insert Multiple Data In MySQL then you are at the right place. In my previous tutorial, we already have a PHP Inserting Data To MySQL. We are going to use this source code and add some codes to have PHP Insert Multiple Data In MySQL.

This is our table that we are going to insert multiple data.

  1. CREATE TABLE `tbl_registration` (<br />
  2. `tbl_registration_id` INT(11) NOT NULL,<br />
  3. `first_name` VARCHAR(100) NOT NULL,<br />
  4. `middle_name` VARCHAR(100) NOT NULL,<br />
  5. `last_name` VARCHAR(100) NOT NULL,<br />
  6. `email` VARCHAR(100) NOT NULL,<br />
  7. `contact_number` VARCHAR(100) NOT NULL<br />
  8. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Inserting Multiple Data Using MySQLi and PDO

Creating Our Connection

For MySQLi (Object-Oriented). Save it as "mysqli_o_o_connection.php".

  1. <?php<br />
  2. $servername = "localhost";<br />
  3. $username = "root";<br />
  4. $password = "";<br />
  5. $dbname = "add_multiple_data";</p>
  6. <p>// Create connection<br />
  7. $conn = new mysqli($servername, $username, $password, $dbname);<br />
  8. // Check connection<br />
  9. if ($conn->connect_error) {<br />
  10. die("Connection failed: " . $conn->connect_error);<br />
  11. }<br />
  12. ?>

For MySQLi (Procedural) Save it as "mysqli_procedural_connection.php".

  1. <?php<br />
  2. $servername = "localhost";<br />
  3. $username = "root";<br />
  4. $password = "";<br />
  5. $dbname = "add_multiple_data";</p>
  6. <p>// Create connection<br />
  7. $conn = mysqli_connect($servername, $username, $password, $dbname);<br />
  8. // Check connection<br />
  9. if (!$conn) {<br />
  10. die("Connection failed: " . mysqli_connect_error());<br />
  11. }<br />
  12. ?>

For PDO (PHP Data Objects) Save it as "pdo_connection.php".

  1. <?php<br />
  2. $servername = "localhost";<br />
  3. $username = "root";<br />
  4. $password = "";<br />
  5. $dbname = "add_multiple_data";</p>
  6. <p>$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);<br />
  7. ?>

PHP Query To Insert Multiple Data

Using MySQLi (Object-Oriented)

  1. <?php<br />
  2. include ('mysqli_o_o_connection.php'); </p>
  3. <p>$sql = "INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)<br />
  4. VALUES ('John', 'Meyer', 'Doe', '[email protected]', '345-2346');";</p>
  5. <p>$sql .= "INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)<br />
  6. VALUES ('Mark', 'Meyer', 'Doe', '[email protected]', '235-5682');";</p>
  7. <p>$sql .= "INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)<br />
  8. VALUES ('Jane', 'Meyer', 'Doe', '[email protected]', '869-2356');";</p>
  9. <p>if ($conn->multi_query($sql) === TRUE) {<br />
  10. echo "Multiple Data Successfully Created!!!";<br />
  11. } else {<br />
  12. echo "Error: " . $sql . "<br>" . $conn->error;<br />
  13. }</p>
  14. <p>$conn->close();<br />
  15. ?>

Using MySQLi (Procedural)

  1. <?php<br />
  2. include ('mysqli_procedural_connection.php'); </p>
  3. <p>$sql .= "INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)<br />
  4. VALUES ('Mark', 'Meyer', 'Doe', '[email protected]', '235-5682');";</p>
  5. <p>$sql = "INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)<br />
  6. VALUES ('John', 'Meyer', 'Doe', '[email protected]', '345-2346');";</p>
  7. <p>$sql .= "INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)<br />
  8. VALUES ('Jane', 'Meyer', 'Doe', '[email protected]', '869-2356');";</p>
  9. <p>if (mysqli_multi_query($conn, $sql)) {<br />
  10. echo "Multiple Data Successfully Created!!!";<br />
  11. } else {<br />
  12. echo "Error: " . $sql . "<br>" . mysqli_error($conn);<br />
  13. }</p>
  14. <p>mysqli_close($conn);<br />
  15. ?>

Using PDO (PHP Data Objects)

  1. <?php<br />
  2. include ('pdo_connection.php');</p>
  3. <p>try {<br />
  4. // set the PDO error mode to exception<br />
  5. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);</p>
  6. <p> // begin the transaction<br />
  7. $conn->beginTransaction();</p>
  8. <p> // our SQL statements<br />
  9. $conn->exec("INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)<br />
  10. VALUES ('Jane', 'Meyer', 'Doe', '[email protected]', '869-2356');");</p>
  11. <p> $conn->exec("INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)<br />
  12. VALUES ('Mark', 'Meyer', 'Doe', '[email protected]', '235-5682');");</p>
  13. <p> $conn->exec("INSERT INTO tbl_registration (first_name, middle_name, last_name, email, contact_number)<br />
  14. VALUES ('John', 'Meyer', 'Doe', '[email protected]', '345-2346');");</p>
  15. <p> // commit the transaction<br />
  16. $conn->commit();<br />
  17. echo "Multiple Data Successfully Created!!!";<br />
  18. }<br />
  19. catch(PDOException $e)<br />
  20. {<br />
  21. // roll back the transaction if something failed<br />
  22. $conn->rollback();<br />
  23. echo "Error: " . $e->getMessage();<br />
  24. }</p>
  25. <p>$conn = null;<br />
  26. ?>

So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment