A JM Posted May 14, 2009 Share Posted May 14, 2009 I have an auto incrementing (ID) field in my database for invoice numbers. Since the 'ID' field doesn't increment until the record is submitted how do I make sure that I have the correct 'ID' for the record I just submitted? If more than one user is submitting an entry this could cause a problem with getting the wrong 'ID' if something sneaks in before I can query the table for the 'ID' How do you guys handle this situation? A JM, Link to comment https://forums.phpfreaks.com/topic/158053-solved-how-to-identify-last-record-inserted-into-mysql/ Share on other sites More sharing options...
ohdang888 Posted May 14, 2009 Share Posted May 14, 2009 queries move so fast that its highly unlikely, nearly impossible, for a regular site to experience that. it should be fine. Link to comment https://forums.phpfreaks.com/topic/158053-solved-how-to-identify-last-record-inserted-into-mysql/#findComment-833711 Share on other sites More sharing options...
Philip Posted May 14, 2009 Share Posted May 14, 2009 mysql_insert_id() And unless you're getting a lot of calls per second, I wouldn't worry too much about it. Link to comment https://forums.phpfreaks.com/topic/158053-solved-how-to-identify-last-record-inserted-into-mysql/#findComment-833712 Share on other sites More sharing options...
A JM Posted May 14, 2009 Author Share Posted May 14, 2009 So what should the process be? Run the INSERT query and then run a query to get the last invoice submitted (SELECT ID FROM claimcue ORDER BY ID DESC LIMIT 1) and then pass that to the Invoice page? A JM, Link to comment https://forums.phpfreaks.com/topic/158053-solved-how-to-identify-last-record-inserted-into-mysql/#findComment-833723 Share on other sites More sharing options...
Philip Posted May 14, 2009 Share Posted May 14, 2009 See the [tweaked] example on the page I linked to: mysql_query("INSERT INTO mytable (product) values ('kossu')"); echo "Last inserted record has id ", mysql_insert_id(); Link to comment https://forums.phpfreaks.com/topic/158053-solved-how-to-identify-last-record-inserted-into-mysql/#findComment-833724 Share on other sites More sharing options...
PFMaBiSmAd Posted May 14, 2009 Share Posted May 14, 2009 Read the link KingPhilip posted above. That function returns the last insert id for the insert query that was just executed by your script. The value is kept per mysql client connection and is 100% accurate no matter how many insert queries are executed. Link to comment https://forums.phpfreaks.com/topic/158053-solved-how-to-identify-last-record-inserted-into-mysql/#findComment-833725 Share on other sites More sharing options...
A JM Posted May 14, 2009 Author Share Posted May 14, 2009 mysql_insert_id() And unless you're getting a lot of calls per second, I wouldn't worry too much about it. Sorry I missed the link... I must be thinking about this in correctly then. From my form submitting the information //set session variable for last invoice generated in the system session_register ("last_invoice"); $last_invoice = mysql_insert_id(); then on my Invoice: <?php echo $_SESSION['last_invoice']; ?> It generated an invoice number butit was incorrect, maybe because of the session itself, not sure here.. A JM, Link to comment https://forums.phpfreaks.com/topic/158053-solved-how-to-identify-last-record-inserted-into-mysql/#findComment-833738 Share on other sites More sharing options...
Philip Posted May 14, 2009 Share Posted May 14, 2009 Make sure to have session_start(); at the top of both scripts. Then, setting the session: $_SESSION['last_invoice'] = mysql_insert_id(); Link to comment https://forums.phpfreaks.com/topic/158053-solved-how-to-identify-last-record-inserted-into-mysql/#findComment-833743 Share on other sites More sharing options...
A JM Posted May 14, 2009 Author Share Posted May 14, 2009 Make sure to have session_start(); at the top of both scripts. Then, setting the session: $_SESSION['last_invoice'] = mysql_insert_id(); Affirmative on the session_start().. I get '0' now as opposed to '50' which I had previously.. does it matter where $_SESSION['last_invoice'] = mysql_insert_id(); is located in the script? Link to comment https://forums.phpfreaks.com/topic/158053-solved-how-to-identify-last-record-inserted-into-mysql/#findComment-833753 Share on other sites More sharing options...
Philip Posted May 14, 2009 Share Posted May 14, 2009 Place it right after your query. Link to comment https://forums.phpfreaks.com/topic/158053-solved-how-to-identify-last-record-inserted-into-mysql/#findComment-833754 Share on other sites More sharing options...
A JM Posted May 14, 2009 Author Share Posted May 14, 2009 That worked but creates a slight problem... I need the 'ID' used in the query for building a file name... any thoughts on how to accomplish that? Will the mysql_insert_id() function work from a select query as well? A JM, Link to comment https://forums.phpfreaks.com/topic/158053-solved-how-to-identify-last-record-inserted-into-mysql/#findComment-833755 Share on other sites More sharing options...
A JM Posted May 14, 2009 Author Share Posted May 14, 2009 Finally figured out a work around by calling a SELECT query just before the INSERT query and incrementing the SELECT +1... Thanks again for all the help and guidance. A JM, Link to comment https://forums.phpfreaks.com/topic/158053-solved-how-to-identify-last-record-inserted-into-mysql/#findComment-833937 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.