Jump to content

Archived

This topic is now archived and is closed to further replies.

delmardata

LAST_INSERT_ID()

Recommended Posts

In the following function I am turning a shopping cart into an order. The order has an order header record and many detail records. I use auto increment to assign the order id and populate the order detail records with order id using the LAST_INSERT_ID() function. Next,I want to store the order id in a session variable but $SESSION['MM_ORDER']=LAST_INSERT_ID(); does not work. what am I doing wrong. Everything else works fine in this function. How does one assign LAST INSERT_UPDATE() to a session variable?

Dennis Brownlie
del mar data systems
dennis@windowareaccounting.net





function MakeOrder($ItemId,$today,$adminid,$oe_id)
{

mysql_query("INSERT INTO inorder(order_id,admin_id,order_date) values (null,'1',NOW())");
"SELECT last_insert_id() as id_number from INCART";
// the next line does not work
$SESSION['MM_ORDER']=LAST_INSERT_ID();
mysql_query("UPDATE incart SET incart.orderid = LAST_INSERT_ID()");

mysql_query("INSERT INTO inorderdtl( order_id,part_id, price, qty) select orderid,part_id, price, qty from incart where cookieId = '" . GetCartId() . "'");


}

Share this post


Link to post
Share on other sites
Did you try to echo the last id? I mean something like this:

[code]$query = "SELECT last_insert_id() as id_number from INCART";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
echo $row['Your_feild_name'];
exit();[/code]

if it will display the last id correctly then we shoud think about sessions...

Share this post


Link to post
Share on other sites
and one more thing, you should use:
$_SESSION instead of $SESSION

Share this post


Link to post
Share on other sites
I am trying to accomplish the exact same thing i.e update header and item table from cart. Is selecting the last insert id from the database really the best method? What if there are other inserts into your order header table at the same time, you may get the order ID for another order and not the one in your current session???

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.