xxreenaxx1 Posted February 18, 2011 Share Posted February 18, 2011 I have a form that ask user to enter other details but the primary key as this is automated and when they click on Submit button it takes the user to different form. now on this form I would like to retreive the primary that has just been added to the table. How can I do this? Quote Link to comment https://forums.phpfreaks.com/topic/228083-how-to-retrieve-a-primary-key/ Share on other sites More sharing options...
ronverdonk Posted February 18, 2011 Share Posted February 18, 2011 Assuming you are talking about MySQL and "the ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established." (PHP manual) you can use the mysql_insert_id() function Quote Link to comment https://forums.phpfreaks.com/topic/228083-how-to-retrieve-a-primary-key/#findComment-1176132 Share on other sites More sharing options...
xxreenaxx1 Posted February 18, 2011 Author Share Posted February 18, 2011 Yes. That works but can I use this printf to save it in a session and use it on a different page. Quote Link to comment https://forums.phpfreaks.com/topic/228083-how-to-retrieve-a-primary-key/#findComment-1176140 Share on other sites More sharing options...
gizmola Posted February 18, 2011 Share Posted February 18, 2011 It's a value. You can save it to a variable and output that variable anyway you want. If you want to store it in a session variable you can do that too. Quote Link to comment https://forums.phpfreaks.com/topic/228083-how-to-retrieve-a-primary-key/#findComment-1176143 Share on other sites More sharing options...
xxreenaxx1 Posted February 18, 2011 Author Share Posted February 18, 2011 Howwould you do that with a printf? I dont know how so help me out please Quote Link to comment https://forums.phpfreaks.com/topic/228083-how-to-retrieve-a-primary-key/#findComment-1176153 Share on other sites More sharing options...
ronverdonk Posted February 18, 2011 Share Posted February 18, 2011 Just do $_SESSION['lastid'] = mysql_insert_id(); Quote Link to comment https://forums.phpfreaks.com/topic/228083-how-to-retrieve-a-primary-key/#findComment-1176234 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.