Jump to content

How to get the next auto-increment value ?


Germaris

Recommended Posts

Due to multiple visitor concurrency, you cannot get the last value and add one and then use the value, because each concurrent visitor can get the same value and there would be a collision.

 

If you have just executed an INSERT query in the same script you can use the mysql_insert_id() function to get the id of that insert - http://www.php.net/manual/en/function.mysql-insert-id.php

 

If you want to get the next id and use it for some purpose before you have all the data necessary, you need to INSERT a row to "reserve" the id and then update the row when all the information is known.

 

It is normally not necessary to get and use the next id. If you rethink what you are doing, you might find that you don't really need to know what the next id is, until you have actually inserted the data in to the database and the actual id has been assigned.

Link to comment
Share on other sites

If you want to get the next id and use it for some purpose before you have all the data necessary, you need to INSERT a row to "reserve" the id and then update the row when all the information is known.

 

Thanks for your reply and for the logic of the ways you explored.

 

You are right.

Except for the last paragraph because you were not aware of my needs (I need to get the next auto_increment value because I need to display this value as it will be also the number of the order/bill BEFORE the customer click to confirm its purchase)

 

All of your post makes sense and, for sure, I’ll use the solution mentioned in the quote.

 

Thanks again!

 

Best regards,

Gerry

Link to comment
Share on other sites

Insert a record with a "pending" value that gets updated when the order is placed/complete. Its the only way to insure the code will work under all conditions. Otherwise you will end up with two orders with the same number by different concurrent visitors.

 

How do you do this? (I'm talking about the "pending" value...)

Link to comment
Share on other sites

Either insert a row that has empty/null values for all the unknown fields or use an existing column and put a value in it that would never occur naturally or add a column to indicate the status of the order, such as - pending, ordered, paid, shipped, complete... A lot of ecommerce scripts would already have a "status" column that could be used.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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