Tutorialsplane

PHP Insert Multiple Record


PHP Insert Multiple Record :– To insert multple records at a time we use concatanation assignment operator.ultiple SQL statements must be executed with the mysqli_multi_query() function.

Inserting Multiple Records Into a Table

The following examples add three new records to the “JohnData” table:

Example // How to create a database table

<?php $servername = "localhost";
$username = "root";
$password = "";
$dbname = "employee";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection
if ($conn-?>connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "INSERT INTO JohnData (firstname, lastname, email)
VALUES ('Adam', 'Smith', 'adam@example.com');";
$sql .= "INSERT INTO JohnData (firstname, lastname, email)
VALUES ('Jorden', 'Dam', 'jorden@example.com');";
$sql .= "INSERT INTO JohnData (firstname, lastname, email)
VALUES ('William', 'Speare', 'william@example.com')";

if ($conn->multi_query($sql) === TRUE) {
    echo "All Three New records added successfully";
} else {
    echo "Error: " . $sql . "<br/>" . $conn->error;
}
$conn->close();
?>

PHP Tutorial