Something like this would work:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\
", mysqli_connect_error());
exit();
}
for ($i = 0; $i < 10; $i++) {
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);
printf ("New Record has id %d.\
", $mysqli->insert_id);
}
/* close connection */
$mysqli->close();
?>
Like StarLion said, you need to execute the query (it’s what the $mysqli->query($query) line does).
Your code is just looping like this :
For 10 times loop and
set the variable $SQL to "insert INTO table (...) values (...)";
End of loop
Execute the $SQL query on database;
See, at the last line, it will execute only use the last value of the SQL variable and execute it on the database.
Again, like StarLion said, it’s way more efficient to use only one query to insert multiple rows with MySQL. From the MySQL doc:
INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas.
Example:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
That way, you only “talk” to the database once, it’s really more efficient than looping and inserting row by row.
A warning:
Be careful when you “craft” a SQL query right from parameters that you didn’t validate. Your code, as I see it now, is probably vulnerable to SQL injections. I wrote about injections on my blog:
http://www.mogosselin.com/injection-explained-and-how-to-fix-them/
Here’s how to use prepared statements to prevent SQL injections:
http://mattbango.com/notebook/code/prepared-statements-in-php-and-mysqli/
Also, make sure that you’re using mysqli_* methods or PDO, NOT mysql_* methods. Read this article on sitepoint for more details: http://www.sitepoint.com/forums/showthread.php?1182768-STOP-using-mysql-migrating-to-procedural-mysqli