Jump to content

Archived

This topic is now archived and is closed to further replies.

Tandem

for loop and sql queries

Recommended Posts

I'm trying to use a for loop to insert records into my database. Here's some theoretical code:

[code]for ($i = 0 ; $i < $num ; $i++) {
$id = mysql_insert_id();
$do_insert = mysql_query("INSERT INTO BLAH (ID) VALUES('$id')");
}[/code]

when i do this, only 1 record is inserted, no matter what the value of $num ($num is always a posive number above 0 btw).

What should i do to make it insert the right numbr of records?

If you don't understand me or need more info just say.

Any help appreciated.

Share this post


Link to post
Share on other sites
because you are trying to insert identical IDs into the table. I assume ID is the primary key.

Share this post


Link to post
Share on other sites
If your ID field is auto_increment then you don't have to worry about it... don't give it a value and MySQL will do it for you or you can pass it null... as follows:

[code]mysql_query("INSERT INTO BLAH (ID) VALUES (NULL)[/code]

Share this post


Link to post
Share on other sites
Also, you call mysql_insert_id() AFTER the insert to find out the value of the newly allocated id.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.