WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP Insert Multiple Records Into MySQL


Insert Multiple Records Into MySQL Using MySQLi and PDO

Multiple SQL proclamations must be executed with the mysqli_multi_query() function.

The following precedents 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
in the event that ($conn->connect_error) {
    die("Connection fizzled: " . $conn->connect_error);
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
Qualities ('John', 'Doe', 'john@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
Qualities ('Mary', 'Moe', 'mary@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
Qualities ('Julie', 'Dooley', 'julie@example.com')";

if ($conn->multi_query($sql) === TRUE) {
    reverberation "New records made successfully";
} else {
    reverberation "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Example (MySQLi Procedural)

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

//Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
/Check connection
on the off chance that (!$conn) {
    die("Connection fizzled: " . mysqli_connect_error());
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
Qualities ('John', 'Doe', 'john@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
Qualities ('Mary', 'Moe', 'mary@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
Qualities ('Julie', 'Dooley', 'julie@example.com')";

if (mysqli_multi_query($conn, $sql)) {
    reverberation "New records made successfully";
} else {
    reverberation "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

The PDO way is a smidgen different:

Example (PDO)

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

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    /set the PDO mistake mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

   /start the transaction
    $conn->beginTransaction();
   /our SQL statements
    $conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
    Qualities ('John', 'Doe', 'john@example.com')");
    $conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
    Qualities ('Mary', 'Moe', 'mary@example.com')");
    $conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
    Qualities ('Julie', 'Dooley', 'julie@example.com')");

    /submit the transaction
    $conn->commit();
    reverberation "New records made successfully";
    }
catch(PDOException $e)
    {
    /move back the exchange if something failed
    $conn->rollback();
    reverberation "Error: " . $e->getMessage();
    }

$conn = null;
?>