0% found this document useful (0 votes)
108 views2 pages

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
108 views2 pages

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
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', '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 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