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.

<?php

$theReceipt = '<table>';

// If the quantity was changed
// This part should be called in some sort of loop 
if(${$quantity})
{
    // This function will have the mysql statement that 
    // inserts the data into the database.
    addItemToShoppingCart($itemDetails); 

    $trackNumber = mysql_insert_id();

    $theReceipt .= '<tr><td>'. $trackNumber. '</td>
       <td>'. $itemDetails['description'] .'</td>
       <td>$'. $itemDetails['price'] .'</td></tr>';
}

$theReceipt .= '</table>';

echo $theReceipt;

?>

If 3 items were selected, then the above code should display something like:

<table>
<tr><td> 1 </td>  
<td> Description for Item #1 </td>
<td>$3.45 </td></tr>
<tr><td> 2 </td>  
<td> Description for Item #2 </td>
<td>$4.35 </td></tr>
<tr><td> 3 </td>  
<td> Description for Item #3 </td>
<td>$1.47 </td></tr>
</table>

You’re welcomed to use the code. You’ll have to write the functions to make it work though. ;) Feel free to comment below if you have questions or suggestions!

Related Links

Related Products:

The Code of the Extraordinary Mind: 10 Unconventional Laws to Redefine Your Life and Succeed On Your Own TermsThe Code of the Extraordinary Mind: 10 Unconventional Laws to Redefine Your Life and Succeed On Your Own Terms

* New York Times Bestseller
* USA Today Bestseller
* Audible Top Ten Non-Fiction Audiobooks

What if everything we think we know about h... Read More >

PHP Cookbook: Solutions & Examples for PHP ProgrammersPHP Cookbook: Solutions & Examples for PHP Programmers

Want to understand a certain PHP programming technique? Or learn how to accomplish a particular task? This cookbook is the first place to look. Wit... Read More >

Bluetooth Speakers,URPOWER Hi-Fi Portable Wireless Stereo Speaker with 7 LED Visual Modes and Build-in Microphone Support Hands-free Function, for iPhone 7 Plus,7,Samsung,Tablets and More-Z3Bluetooth Speakers,URPOWER Hi-Fi Portable Wireless Stereo Speaker with 7 LED Visual Modes and Build-in Microphone Support Hands-free Function, for iPhone 7 Plus,7,Samsung,Tablets and More-Z3URPOWER-Z3 Bluetooth Speaker

The URPOWER-Z3 Bluetooth speaker offers great quality sound with Bluetooth 3.0 technology. With the ultra-compac... Read More >
Related Posts Plugin for WordPress, Blogger...