Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/10186-getting-multiple-insert-ids/
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>";
}
[!--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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.