Home >Database >Mysql Tutorial >How Can I Insert Multiple Rows into a MySQL Table with a Single Query?
Multiple Row Insertion in MySQL with a Single Query
You have expressed a need to perform multiple insertions into a MySQL table with a single query. This can be achieved efficiently using the following steps:
Construct the Query:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...), (value3, value4, ...), ...;
Specify Multiple Rows:
INSERT INTO pxlot (realname, email, address, phone, status, regtime, ip) VALUES ('realname1', 'email1', 'address1', 'phone1', 0, 'dateTime1', 'ip1'), ('realname2', 'email2', 'address2', 'phone2', 0, 'dateTime2', 'ip2'), ('realname3', 'email3', 'address3', 'phone3', 0, 'dateTime3', 'ip3');
Execute the Query:
Example:
$query = "INSERT INTO pxlot (realname, email, address, phone, status, regtime, ip) VALUES ('realname1', 'email1', 'address1', 'phone1', 0, 'dateTime1', 'ip1'), ('realname2', 'email2', 'address2', 'phone2', 0, 'dateTime2', 'ip2'), ('realname3', 'email3', 'address3', 'phone3', 0, 'dateTime3', 'ip3');"; $result = mysql_query($query); if ($result) { echo "Data inserted successfully."; } else { echo "Error: " . mysql_error(); }
The above is the detailed content of How Can I Insert Multiple Rows into a MySQL Table with a Single Query?. For more information, please follow other related articles on the PHP Chinese website!