We can insert multiple records in database using PHP. To do this, suppose we have database *example* having table demo with columns firstname, lastname, email. We need to insert multiple record in the table by using PHP. To insert the multiple records in the table:
<?php
$hostname = "localhost";
$username = "username";
$password = "password";
$dbname = "example";
// Create connection
$conn = new mysqli($hostname, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO demo (firstname, lastname, email)
VALUES ('fname1', 'lname1', 'ex1@example.com');";
$sql .= "INSERT INTO demo (firstname, lastname, email)
VALUES ('fname2', 'lname2', 'ex2@example.com');";
$sql .= "INSERT INTO demo (firstname, lastname, email)
VALUES ('fname3', 'lname3', 'ex3@example.com')";
if ($conn->multi_query($sql) === TRUE) {
echo "New records created successfully";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
$conn->close();
?>
0 Comment(s)