Jump to content


Photo

Getting Multiple Insert IDs


  • Please log in to reply
2 replies to this topic

#1 cyberRobot

cyberRobot
  • Moderators
  • Much to Learn
  • 2,771 posts

Posted 22 May 2006 - 02:50 PM

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:
INSERT INTO authors (name, org, title, email, phone) VALUES ('name1', 'org1', 'title1', 'email1', 'phone1'), ('name2', 'org2', 'title2', 'email2', 'phone2');


I normally use:
mysql_insert_id()

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


Thanks

#2 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 22 May 2006 - 03:12 PM

why dont you try something like this:

[code=auto:0]

$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>";
}
http://thewebsolutionprovider.com - A Solution For Everything Web!

#3 cyberRobot

cyberRobot
  • Moderators
  • Much to Learn
  • 2,771 posts

Posted 22 May 2006 - 03:33 PM

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

[code=auto:0]

$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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users