Jump to content

[SOLVED] How to Identify last record INSERTED into mySQL..


A JM

Recommended Posts

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,

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.

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,

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?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.