john2121 Posted February 16, 2011 Share Posted February 16, 2011 I am trying to save LAST_INSERT_ID() to a session variable for later use but not with much success. This doesn't work. $_SESSION['lastid'] = LAST_INSERT_ID(); Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/ Share on other sites More sharing options...
Pikachu2000 Posted February 16, 2011 Share Posted February 16, 2011 Actually, it's mysql_insert_id. Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/#findComment-1175068 Share on other sites More sharing options...
Maq Posted February 16, 2011 Share Posted February 16, 2011 Try using - mysql_insert_id. Keep in mind you need a connection open. Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/#findComment-1175069 Share on other sites More sharing options...
Pikachu2000 Posted February 16, 2011 Share Posted February 16, 2011 I guess I should qualify that. It's mysql_insert_id() IF you're using MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/#findComment-1175070 Share on other sites More sharing options...
Maq Posted February 16, 2011 Share Posted February 16, 2011 LAST_INSERT_ID() is a MySQL function, not PHP. Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/#findComment-1175072 Share on other sites More sharing options...
john2121 Posted February 16, 2011 Author Share Posted February 16, 2011 Thank you, guys! However, I tried $lastid = mysql_insertid() but it only inserts in the second table. When I try to use that in a third table the value ends up being a zero. Is this variable valid for inserting into one table only? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/#findComment-1175223 Share on other sites More sharing options...
Maq Posted February 16, 2011 Share Posted February 16, 2011 Thank you, guys! However, I tried $lastid = mysql_insertid() but it only inserts in the second table. When I try to use that in a third table the value ends up being a zero. Is this variable valid for inserting into one table only? Thanks! Can you post the relevant code? Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/#findComment-1175224 Share on other sites More sharing options...
Pikachu2000 Posted February 16, 2011 Share Posted February 16, 2011 Shouldn't be, since you're assigning the value to a variable. Can you post the relevant code? Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/#findComment-1175225 Share on other sites More sharing options...
john2121 Posted February 16, 2011 Author Share Posted February 16, 2011 Actually problem got worse. The code below gives me the second last id, not the last one. if (!$connProj) { die('Could not connect: ' . mysql_error()); } mysql_select_db("databasename", $connProj); $sql="INSERT INTO Submitter (Name, SNL_ID,Org, Email) VALUES ('$name', '$snlid', '$org', '$email')"; $lastid = mysql_insert_id(); echo "last id: " . $lastid; if (!mysql_query($sql,$connProj)) { die('Error: ' . mysql_error()); } So after doing the insert the browser might show me 80 as the last id but in the database the last id is 81. I have checked it many times because I couldn't believe this could happen. Thank you so much for trying to help. Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/#findComment-1175361 Share on other sites More sharing options...
xylex Posted February 16, 2011 Share Posted February 16, 2011 Execute the insert query before your get the last insert id, not after. Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/#findComment-1175365 Share on other sites More sharing options...
john2121 Posted February 16, 2011 Author Share Posted February 16, 2011 Isn't that what I am doing i.e. executing the insert query before getting the last insert id? I am at my wits end because the same code on another page gives a last id of zero the first time and the second last one after I click the enter button in the address bar to reload the page. I am thoroughly confused. I acknowledge I must be a bit dense but I thought mysql_insert_id() was supposed to give the last id. Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/#findComment-1175368 Share on other sites More sharing options...
Pikachu2000 Posted February 17, 2011 Share Posted February 17, 2011 mysql_query() executes the query. You have mysql_insert_id() before that. Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/#findComment-1175369 Share on other sites More sharing options...
john2121 Posted February 17, 2011 Author Share Posted February 17, 2011 I have wasted so much time of so many of you good folks here just because I didn't understand something so basic. Thanks to all who tried to help and most especially to Pikachu who finally showed me the light. Thank you, guys and my apologies for testing your patience. Quote Link to comment https://forums.phpfreaks.com/topic/227885-saving-last_insert_id-to-a-session-variable/#findComment-1175371 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.