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:

Bluetooth Speakers, Portable Wireless Stereo Speaker with Built-in Microphone Support Hands-free Function, FM Radio, 2x5W Acoustic Drivers, 12 Hours Playtime(Black)Bluetooth Speakers, Portable Wireless Stereo Speaker with Built-in Microphone Support Hands-free Function, FM Radio, 2x5W Acoustic Drivers, 12 Hours Playtime(Black)SUPERIOR SOUND QUALITY
Experience your music in full-bodied stereo and enjoy a High Definition stereo sound with a impressive volume. Up to 2X volu... Read More >
Clean Code: A Handbook of Agile Software CraftsmanshipClean Code: A Handbook of Agile Software CraftsmanshipEven bad code can function. But if code isn’t clean, it can bring a development organization to its knees. Every year, countless hours and significa... Read More >
CodenamesCodenamesCodenames is a social word game with a simple premise and challenging game play. Two rival spymasters know the secret identities of 25 agents. Their t... Read More >