In MySQL, you can insert multiple rows into a table in a single query by separating each set of values with commas. This approach is highly efficient and commonly used in PHP-based applications to reduce database calls.
INSERT statement can add many records
// General syntax for inserting multiple rows
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3),
(value1, value2, value3),
(value1, value2, value3);
// Insert multiple student records in one query
INSERT INTO students (name, age, grade)
VALUES ('John Doe', 21, 'B'),
('Jane Smith', 22, 'A'),
('Alice Brown', 23, 'C');
All three student records are inserted into the students table at once. MySQL treats each set of values as a separate row but executes the operation in a single database call.
// PHP example using MySQLi to insert multiple rows
$sql = "INSERT INTO students (name, age, grade)
VALUES ('Bob White', 25, 'B'),
('Carol Green', 24, 'A'),
('David Black', 22, 'C')";
mysqli_query($conn, $sql);
products table with name, price, and stockSELECT *