PHP MySQL Insert Multiple is a feature that allows you to insert multiple records into a MySQL database using a single query. This feature is very useful when you need to insert a large number of records into a database at once. It saves time and reduces the number of queries that need to be executed.
The syntax for inserting multiple records into a MySQL database using PHP is as follows:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES
(value1, value2, value3, ...),
(value1, value2, value3, ...),
(value1, value2, value3, ...),
...
The above syntax inserts multiple records into the table_name table. The column1, column2, column3, ... are the names of the columns in the table. The values for each column are specified in the VALUES clause. Each set of values is enclosed in parentheses and separated by commas.
Here is an example of how to use PHP MySQL Insert Multiple:
<?php
// Connect to MySQL database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Insert multiple records into the table
$sql = "INSERT INTO users (name, email, age)
VALUES
('John Doe', 'john@example.com', 25),
('Jane Doe', 'jane@example.com', 30),
('Bob Smith', 'bob@example.com', 35),
('Mary Johnson', 'mary@example.com', 40)";
if (mysqli_query($conn, $sql)) {
echo "Records inserted successfully.";
} else {
echo "Error: " . $sql . "
" . mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
In the above example, we are inserting four records into the users table. The name, email, and age columns are specified in the INSERT INTO clause. The values for each column are specified in the VALUES clause. The mysqli_query() function executes the query and returns true if the query was successful, or false if there was an error.
PHP MySQL Insert Multiple is a powerful feature that can save you a lot of time and effort when working with large databases. It is easy to use and can be implemented in just a few lines of code.