Jump to content

Saving LAST_INSERT_ID() to a session variable


john2121

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.