In this unit, we will see how to insert multiple records in the database table. Insert statement adds new records into a table.
Read Also: PHP MySQL Insert Data
Index
Syntax
INSERT INTO table_name (column_list) VALUES (value_list_1), (value_list_2), ... (value_list_n);
If a string data types to be inserted, double or single quotes are to be used.
When adding a new row, the datatype of the value and the column should match.
There are three ways of inserting multiple rows.
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "user"; // Create connection $connection = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$connection) { die("Connection failed: " . mysqli_connect_error()); } $query = "INSERT INTO user (firstname, lastname, email) VALUES ('Neha', 'shah', 'neha@gmail.com');"; $query.= "INSERT INTO user (firstname, lastname, email) VALUES ('Mary', 'shah', 'mary@gmail.com');"; $query.= "INSERT INTO user (firstname, lastname, email) VALUES ('rita', 'Ahuja', 'rita@gmail.com')"; if (mysqli_multi_query($connection, $sql)) { echo "New records created successfully"; } else { echo "Error: " . $query . "<br>" . mysqli_error($connection); } mysqli_close($connection); ?>
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "user"; // Create connection $connection = new mysqli($servername, $username, $password, $dbname); // Check connection if ($connection->connect_error) { die("Connection failed: " . $connection->connect_error); } $query = "INSERT INTO user (firstname, lastname, email) VALUES ('Neha', 'Shah', 'neha@gmail.com');"; $query .= "INSERT INTO user (firstname, lastname, email) VALUES ('Mary', 'Shah', 'mary@gmail.com');"; $query .= "INSERT INTO user (firstname, lastname, email) VALUES ('Rita', 'Ahuja', 'rita@gmail.com')"; if ($connection->multi_query($query) === TRUE) { echo "New records created successfully"; } else { echo "Error: " . $query . "<br>" . $connection->error; } $connection->close(); ?>
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "user"; try { $connection = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // begin the transaction $connection->beginTransaction(); // our SQL statements $connection->exec("INSERT INTO user (firstname, lastname, email) VALUES ('Neha', 'shah', 'neha@gmail.com')"); $connection->exec("INSERT INTO user (firstname, lastname, email) VALUES ('Mary', 'shah', 'mary@gmail.com')"); $connection->exec("INSERT INTO user (firstname, lastname, email) VALUES ('Rita', 'Ahuja', 'julie@gmail.com')"); // commit the transaction $connection->commit(); echo "New records created successfully"; } catch(PDOException $e) { // roll back the transaction if something failed $connection->rollback(); echo "Error: " . $e->getMessage(); } $connection = null; ?>
Read More: PHP MySQL Select Data
In this article, we learned how to insert multiple rows using the mysqli_multi_query() function. When adding a new row, the datatype of the value and the column should match.
We hope you found this article helpful and in case, you want to learn about how to connect to MySQL using PHP and creating databases, do check out our previous blogs. Happy Learning 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…