Forum Moderators: coopster

Message Too Old, No Replies

Assigning Order_ID value to BEFORE posting data

         

michlcamp

5:16 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



Is there a way to assign an ID to form data before it's posted to a data table?

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

coopster

8:40 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, the second question you have already answered ... yes, you can pass form values to the next display in the sequence via hidden form values. Or you can store it in a cookie, or a session ...

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?

michlcamp

9:19 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



yeah, I've decided to go ahead and post and then query the order_id for the additional forms.

coopster

9:31 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Don't forget that you can get that newly-generated id value right away in the script that does the posting to the database. You just query it right after you have done the INSERT operation.

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');
...

Something along those lines ...

dmmh

10:05 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



or, by far easier, use the predefined PHP function for this

$insert_id = msql_insert_id() [nl3.php.net];

assuming you use mysql :)

a function very few people know

coopster

11:02 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Best not to though, dmmh. There is a caution on the PHP manual pages against that.


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...]

dmmh

7:36 am on Jan 6, 2006 (gmt 0)

10+ Year Member



wow, didnt even know that, thank you :)