0% found this document useful (0 votes)
130 views

PHP MySQL Insert Multiple Records

This document discusses how to insert multiple records into a MySQL database table at once using PHP. It shows an example of using the mysqli_multi_query() function to execute multiple INSERT SQL statements separated by semicolons. This inserts three new records into the "MyGuests" table with names and emails.

Uploaded by

Saripilli Vasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

PHP MySQL Insert Multiple Records

This document discusses how to insert multiple records into a MySQL database table at once using PHP. It shows an example of using the mysqli_multi_query() function to execute multiple INSERT SQL statements separated by semicolons. This inserts three new records into the "MyGuests" table with names and emails.

Uploaded by

Saripilli Vasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PHP 

MySQL Insert Multiple


Records
❮ PreviousNext ❯

Insert Multiple Records Into MySQL Using


MySQLi and PDO
Multiple SQL statements must be executed with
the mysqli_multi_query() function.

The following examples add three new records to the "MyGuests" table:

Example (MySQLi Object-oriented)


<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)


VALUES ('John', 'Doe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', '[email protected]')";

if ($conn->multi_query($sql) === TRUE) {


  echo "New records created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

Note that each SQL statement must be separated by a semicolon.

You might also like