Forum Moderators: coopster
I have a form which collects data for product sales but want to automatically assign the order_id value before the form data is posted - in other words, not using an 'auto-incrementing' field in the table as it posts. (I'm using that for something else).
The order_id value would have to be the NEXT number after the previous order.
and, is there a way I can pass that value to the next form using a hidden field?
thanks in advance,
mc
To answer the first question though, that may not be a very good idea. What are you going to do when user A is sitting on the display for two hours with the "previous order_id + 1" value stored in their hidden form field? In the meantime, one hundred other people have posted their orders using that value? See what I mean?
Is there a reason the user needs to see the order number before you post the order?
For example,
mysql_query("INSERT INTO mytable (myid, mycolumn1, mycolumn2)
VALUES(NULL, 'myvalue1', 'myvalue2')");
$rows = mysql_query('SELECT LAST_INSERT_ID() AS lastInsertID FROM mytable');
... $insert_id = msql_insert_id() [nl3.php.net];
assuming you use mysql :)
a function very few people know
Caution
mysql_insert_id() [php.net] converts the return type of the native MySQL C API function mysql_insert_id() to a type of long (named int in PHP). If your AUTO_INCREMENT column has a column type of BIGINT, the value returned by mysql_insert_id() will be incorrect. Instead, use the internal MySQL SQL function LAST_INSERT_ID() in an SQL query.
A relative thread ...
[webmasterworld.com...]