PHP: mysql_insert_id()

July 16, 2009 by  
Filed under Tips and Tutorials

tn-phpSometimes it is necessary to get the ID value of the last record you inserted into a MySQL database.

For example, let’s say you have a shopping cart function that added records into the database. But you need to print out a receipt that includes the item tracking number. And this tracking number would be the record’s ID value, which is also the PRIMARY KEY of the table.

An easy way to do this in PHP is by using the function mysql_insert_id().
NOTE: This will only work for AUTO_INCREMENT fields.

Pretend STEP 1 is a form with a list of items. If you change the QUANTITY of the item, and submit, you will be at STEP 2, which is the sample code below.
Read more

Related Products: