PHP Insert Multiple Records Into MYSQL

Good day everyone! Today, I’m gonna teach you on PHP Insert Multiple Records Into 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.

<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'your_db_name';

$mysqli = new mysqli($servername, $username, $password, $dbname);

if ($mysqli->connect_error) {
die("Connection Failed! " . $mysqli->connect_error);
}
?>

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

$sql = "INSERT INTO names (firstname, lastname) VALUES ('Harley', 'Lumagui')";
$sql .= "INSERT INTO names (firstname, lastname) VALUES ('Juan', 'Dela Cruz')";
$sql .= "INSERT INTO names (firstname, lastname) VALUES ('Pedro', 'Penduko')";

if ($mysqli->multiple_query($sql) == true) {
echo 'New Records Successfully Inserted!';
} else {
echo 'Error: ' . $sql. '<br>' . $mysqli->error;
}

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.

Leave a Comment