Jump to content


Photo

LAST_INSERT_ID()


  • Please log in to reply
3 replies to this topic

#1 delmardata

delmardata
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 14 September 2006 - 11:47 PM

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


}


#2 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 14 September 2006 - 11:53 PM

Did you try to echo the last id? I mean something like this:

$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();

if it will display the last id correctly then we shoud think about sessions...
I don't need your script, I'll try to write it myself

#3 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 14 September 2006 - 11:55 PM

and one more thing, you should use:
$_SESSION instead of $SESSION
I don't need your script, I'll try to write it myself

#4 mo

mo
  • Members
  • PipPipPip
  • Advanced Member
  • 139 posts

Posted 03 August 2007 - 12:46 PM

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???




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users