Forum Moderators: coopster
I have a data entry form where the user enters information. I use two buttons on this form. The first called Autonumber which uses mysql_insert_id() to pull the number from my MySQL table for the next record to be entered, and another button called Save which allows the user to save the record to the table. My problem is two fold if the user hits Autonumber twice, the first submit pulls a new number from the table and the second submit saves the record. However if the user then hits Save the record is duplicated. This also presents a problem if the user forgets to add a piece of information or wishes to change something after they hit save because it just duplicates the record.
How would I like it to work? If possible I would like to eliminate the AutoNumber button and automatically retrieve the next available number to be assigned when the user opens the form. And, I would like to allow the user to edit the information in the form if they choose to even after they hit Save. I will post relevant portions of the code if that would be helpful, but I really don't want to force someone to read my whole page. Thanks Have a Great Day!
Why don't you just insert the user data, then get the row id for the last insertion and show it to the user on a confirmation screen?
An alternative would be to simply create a new row in the DB upon opening a "new record" form and the script would always update and never insert. If you went this route, though, you would need some means of pruning empty rows. One possiblity would be to check whether the last row is empty and, if so, use that one. If not, create a new one. Without understanding the situation though, this all sounds unnecessarily complicated and roundabout.
Also, be aware that though not deprecated, you should keep in mind some of the limitations of mysql_insert_id() [php.net]. In some cases it may be preferable to use the MySQL SQL function LAST_INSERT_ID().