Jump to content

Archived

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

cyberRobot

Getting Multiple Insert IDs

Recommended Posts

Is there any way to get multiple IDs when performing an INSERT statement which inserts more than one row?

Our INSERT statement looks something like:
[code]
INSERT INTO authors (name, org, title, email, phone) VALUES ('name1', 'org1', 'title1', 'email1', 'phone1'), ('name2', 'org2', 'title2', 'email2', 'phone2');
[/code]


I normally use:
[code]
mysql_insert_id()
[/code]

...to get the insert id, but this only gives me the ID for the first insert.


Thanks

Share this post


Link to post
Share on other sites
why dont you try something like this:

[code]

$name[0] = "Bob";
$org[0] = "Builders Ltd";
$title[0] = "Mr";
$email[0] = "bob@buildersltd.com";
$phone[0] = "01578968345";

for ($i=0; $i < $number_of_inserts; $i++) {
mysql_query ("INSERT INTO authors VALUES ('$name[$i]','$org[$i]','$title[$i]','$email[$i]','$phone[$i]'");
$id_list[$i] = mysql_insert_id();
}

foreach ($id_list as $id) {
echo $id . "<br>";
}

Share this post


Link to post
Share on other sites
[!--quoteo(post=376051:date=May 22 2006, 10:12 AM:name=glenelkins)--][div class=\'quotetop\']QUOTE(glenelkins @ May 22 2006, 10:12 AM) [snapback]376051[/snapback][/div][div class=\'quotemain\'][!--quotec--]
why dont you try something like this:

[code]

$name[0] = "Bob";
$org[0] = "Builders Ltd";
$title[0] = "Mr";
$email[0] = "bob@buildersltd.com";
$phone[0] = "01578968345";

for ($i=0; $i < $number_of_inserts; $i++) {
mysql_query ("INSERT INTO authors VALUES ('$name[$i]','$org[$i]','$title[$i]','$email[$i]','$phone[$i]'");
$id_list[$i] = mysql_insert_id();
}

foreach ($id_list as $id) {
echo $id . "<br>";
}
[/quote]


I already have something like that, but I would like to get away from all the for loops. I have one loop to read the info from the online form, another to store the info to the database, and another one to strip the slashes from the input and print the info to the screen.

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.