Forum Moderators: coopster
$cart_id = $this->get_cart_id();
if(!$cart_id){
// If no cart id found generate one
$unique_cid = md5(uniqid(rand (),1));
// Set cart id into the cookie
setcookie('cid',$unique_cid,time()+24*3600*60);
// Register session with cart id value
$_SESSION['cid'] = $_COOKIE['cid'];
// If person is a member
// modify their profile with
// cart id in the database
if($_SESSION['login']){
$_SESSION['cid'] = $_COOKIE['cid'];
@mysql_query("UPDATE members SET cart_id = '{$_SESSION['cid']}' WHERE id = '".$_SESSION['userid']."'");
}
}
The code above checks to see if a cart ID exists. If a unique cart ID does not exist, it will generate one and assign it $_SESSION['cid'].
Later in the cart_add Function it will utilize $_SESSION['cid'] in the following code:
if($product_qty>0){
$sql = mysql_query("INSERT INTO shopping_carts(cart_identifier,product_id,product_title,product_qty,product_price,date) VALUES('{$_SESSION['cid']}','$product_id','{$products['product_title']}','$product_qty','{$products['product_price']}',NOW())");
}else{
$sql = FALSE;
}
The problem is that the $_SESSION['cid'] value is not getting assigned to the cart_identifier field of the shopping_carts table in my database the first time(after $_SESSION['cid'] gets assigned the cart ID). However, the second time and thereafter that the cart_add Function gets called, the $_SESSION['cid'] does gets assigned to the cart_identifier field of the shopping_carts table. Does anybody know why this is happening? Thanks.
@mysql_query("UPDATE members SET cart_id = [b]'{$_SESSION['cid']}'[/b] WHERE id = '".$_SESSION['userid']."'"); The problem is that you are using apostrophes within apostrophes for both queries, and that's breaking the query. You might want to assign a new variable to hold the values you want to use in the queries:
$curr_cid="{".$_SESSION['cid']."}"; @mysql_query("UPDATE members SET cart_id = '$curr_cid' WHERE id = '".$_SESSION['userid']."'"); Similarly (although not a problem with the queries):
$curr_cid="{".$_SESSION['cid']."}"; $curr_uid=$_SESSION['userid']; @mysql_query("UPDATE members SET cart_id = '$curr_cid' WHERE id = '$curr_uid'"); It's a little amazing that the INSERT query works at all, given the nested apostrophes therein.
Hope that helps!
So, what i am saying is that when i try to assign the value of $_SESSION['cid'] to something the first time after registering it, that variable that i am trying to assign it to is not getting assigned the value of $_SESSION['cid']. However, the next i try and assign the value of $_SESSION['cid'] to something, it works.