cyberRobot Posted May 22, 2006 Share Posted May 22, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/10186-getting-multiple-insert-ids/ Share on other sites More sharing options...
glenelkins Posted May 22, 2006 Share Posted May 22, 2006 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 Link to comment https://forums.phpfreaks.com/topic/10186-getting-multiple-insert-ids/#findComment-37964 Share on other sites More sharing options...
cyberRobot Posted May 22, 2006 Author Share Posted May 22, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/10186-getting-multiple-insert-ids/#findComment-37971 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.