This topic is locked
[SOLVED]

 Show next ID number BEFORE add

5/8/2011 10:05:21 PM
PHPRunner General questions
bbarker author

My New Vendor page works good. But it doesn't add the auto-incremented VendorID until AFTER the record is saved.
My customer is insisting that he needs to be able to SEE the new VendorID number while he's filling out the form (BEFORE he saves the record).
Any ideas on how to do this?
Should I display something like this?

$_SESSION["VendorID"]= mysql_insert_id() + 1;
D
danaci 5/9/2011

select max(id) maxID from your_table

Sergey Kornilov admin 5/9/2011

Correct SQL Query to execute is

select max(id)+1 as maxID from your_table


Please note that if two users open Add page at about the same time they both will be shown the same 'next ID' however only first user is going to get this ID. You need to communicate this to your customer.

E
electromotive 5/12/2011



select max(id) maxID from your_table



This is not a robust or recommended solution.

There is the multi-user contention issue raised above, and also its always possible to abort the add, or go to lunch during the add.
There are several possible solutions to this which work in a multi-user environment and if the transaction gets delayed or aborted.
If you insist on using the autoincrement (primary key) as your vendor number then you will need to actually create the record and reserve the number before the add. You'll need to add a state field to the record to say that the new record is 'pending' and maybe who is holding the number. Once you complete the add (which technically is now an update), you update the state field to indicate that the record is now 'active'. If the add never completes then the record remains inactive and can even be deleted at a future stage.

bbarker author 5/13/2011



There are several possible solutions to this which work in a multi-user environment and if the transaction gets delayed or aborted.
...Once you complete the add (which technically is now an update), you update the state field to indicate that the record is now 'active'.


Thanks for the comments. I have it working as suggested earlier.
My original post was intended to solicit suggestions for options. While the +1 display "works", it does raise issues if more than one person is inputting data. I had thought about an "event" which saves the new Blank record and then re-displays the screen (technically as an EDIT page)... But this seemed too cumbersome for now. I'm sure interested in any other ideas -- but for now I'm displaying the "Next Record Number" while they enter the data.

T
tedwilder 5/18/2011

.