I have a table that stores orders.
id (auto increment) an oid (not auto incremented) and then the order details.
some orders can have multiple entries, this is why i have an ID and an Oid(order id)
so if you order 3 xwidgets and 4Zwidgets they go under the same OID
I am using the following code to assign the next OID number and am wondering if this is the best way:
$maxquery = mysql_query("SELECT MAX(oid) FROM ordr") or die(mysql_error());
$maxresult = mysql_fetch_array($maxquery);
$maxid = $maxresult['MAX(oid)'];
$oid = $maxid + 1; -- this gives me a new OID for the next entry since i can not auto increment this column
You probably want to use auto-increment columns. This will let you insert the data, and MySQL will pick an index for you. You’ll need to denormalize your data to do this though.
You’ll want two tables:
ordersandorder_items, where:Insert the order into the
orderstable with aNULLorder_idand record theorder_idthat MySQL sets. Now insert theorder_itemusing theorder_idyou have, and let it auto-generate theitem_id.This way, MySQL will generate the numbers, so you don’t need to worry about two orders trying to use the same order ID. Also, any order information won’t be duplicated for each item in the order.