PHP Insert Multiple Records Into MYSQL

0
67

Good day everyone! Today, I’m gonna teach you on inserting multiple data or records in MYSQL.

Yes, as what you read it is possible to insert multiple data in MYSQL. If you already know it, that’s good. But if you still wondering how does it make, well, this is the best article for you.

So lets start now.

So first, you must create your connection first. Copy the following PHP code to your php file.

  1. <?php
  2. $servername = 'localhost';
  3. $username = 'root';
  4. $password = '';
  5. $dbname = 'your_db_name';
  6.  
  7. $mysqli = new mysqli($servername, $username, $password, $dbname);
  8.  
  9. if ($mysqli->connect_error) {
  10. die("Connection Failed! " . $mysqli->connect_error);
  11. }
  12. ?>

Now, for inserting the multiple records. Put the following codes on your PHP page.

  1. $sql = "INSERT INTO names (firstname, lastname) VALUES ('Harley', 'Lumagui')";
  2. $sql .= "INSERT INTO names (firstname, lastname) VALUES ('Juan', 'Dela Cruz')";
  3. $sql .= "INSERT INTO names (firstname, lastname) VALUES ('Pedro', 'Penduko')";
  4.  
  5. if ($mysqli->multiple_query($sql) == true) {
  6. echo 'New Records Successfully Inserted!';
  7. } else {
  8. echo 'Error: ' . $sql. '<br>' . $mysqli->error;
  9. }

End.

If you have questions regarding these article entitled “PHP Insert Multiple Records Into MYSQL”, feel free to ask us by commenting below or visit on our contact page. Thank you.

Facebook Comments
(Visited 75 times, 2 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here