-1

I'm trying to put multiple records in my database using php and mysql. I'm able to do the first INSERT if I don't try also doing the second one. Once I add the second insert, no record is written at all. I'm think that it is a syntax error but I tried very clear examples from http://www.w3schools.com/php/php_mysql_insert_multiple.asp and insert multiple rows via a php array into mysql but still not working. The only difference is the concatination and the semi-colins at the end of the statement.

Any ideas what I may be doing wrong?

 $query="INSERT INTO  myTable (
 `user1` ,
 `user2` ,
 `user3` 
 )
 VALUES ('data1', 'data2' , 'data3');";

   $query .="INSERT INTO  myTable (
 `user1` ,
 `user2` ,
 `user3` 
 )
 VALUES ('data4', 'data5' , 'data6')";
Community
  • 1
  • 1
CloudyKooper
  • 727
  • 3
  • 19
  • 47
  • that first w3schools example is why we generally say not to use w3schools, it has some terrible code. the 2nd link has a great example, but you completely ignoring it. –  Oct 18 '15 at 22:35
  • I tried the example from the second link. I did not ignore it. – CloudyKooper Oct 19 '15 at 00:28

1 Answers1

6

You don't write separate queries and append them together (unless you're using mysqli_multi_query which those links suggest you are not doing and that would be inefficient anyway). You only need a comma separated list of values to be inserted once you have constructed your INSERT statement:

// Removed semi-colon terminating the query
$query="INSERT INTO  myTable (
 `user1` ,
 `user2` ,
 `user3` 
 )
 VALUES ('data1', 'data2' , 'data3')";

// removed insert statement, added required leading comma
$query .=",('data4', 'data5' , 'data6')";
John Conde
  • 217,595
  • 99
  • 455
  • 496