I am stumped with this code

I did not write this code, but it seems to have started causing a fatal error, though it hasn’t changed that I know of.

here is the code:

<?
 require "./includes/cart_class.php";
 require_once "./includes/functions.inc.php";
 session_start();
 //session_unregister('cart');
 //exit();

     $product_id                = $_POST["pid"];
     $pcid                    = $_POST["pcid"];
     $product_name            = $_POST["product_name"];
     $price                    = $_POST["price"];
     $qty                    = $_POST["qty"];
     $att_id                    = $_POST["att_id"];
     $att_value_id            = $_POST["att_value_id"];
     $pop                    = $_POST['pop'];  /// if value =1 then comes from popup product details page.
     $page_type                = $_POST['page_type'];  /// if value =1 then comes from category pages.
     $stoolH                 = $_POST["stoolheight"];

     $arr_att = array($att_id, $att_value_id);

     $sql="select product_weight from yp_product where product_id='$product_id'";
     $pweight=getSingleResult($sql);


     if($_SESSION['cart']!=""){
         $cart=$_SESSION['cart'];
     }

     if($qty==''){
         if($min_qty!=""){
             $qty=$min_qty;
         }else{
             $qty=1;
         }
     }


     if($qty==0){
         $_SESSION['sess_msg']= "QUANTITY CANNOT BE LESS THAN 1";
         header("Location: product_detail.php?pid=$pid&pcid=$pcid");
         exit();
     }

     if(!session_is_registered('cart')){
         $cart =  new cart();
         session_register('cart');
     }

     $item_object =  new product($product_id  , $product_name , $price, $arr_att, $qty , $pweight, $stoolH);
     $cart->addToCart($item_object);

 $back="modify_cart_frm.php";
 header("Location: ".$back);
 exit;

 ?>

and this is the fatal error that you get when adding an item to the shopping cart AFTER you have already tried and were told your cart was empty. So the fatal error occurs after the second attempt at adding an item to your cart.

Fatal error: Call to a member function addToCart() on a non-object in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 50

Is this code wrong? Does it look right?

When storing an object into a session it is stored serialized, it is then unserialized when you retrieve it again.

In your case if cart exist in the session, then you should assign it to the variable $cart. But clearly something here is iffy because later on you get an error saying this isn’t an object anymore.

Check what these values really are
[php] if($_SESSION[‘cart’]!=""){
var_dump($_SESSION[‘cart’]);
var_dump($cart);
$cart=$_SESSION[‘cart’];
}[/php]

Note how much better it is to use the php code tags (right next to the code tag) here on the forums :slight_smile:

If the dumped data doesn’t help you, please post them back here.

i’ll use that php code next time. sorry.

here is the error i get after i used a dump

Notice: Undefined index: att_id in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 18

Notice: Undefined index: pop in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 20

Notice: Undefined index: page_type in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 21

Notice: Undefined index: stoolheight in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 22

Notice: Undefined index: cart in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 30

Deprecated: Function session_is_registered() is deprecated in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 49

Deprecated: Function session_register() is deprecated in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 51
object(cart)#1 (1) { [“cart”]=> array(0) { } }
Deprecated: Function session_unregister() is deprecated in /data/18/1/57/42/1220042/user/1300782/htdocs/includes/cart_class.php on line 211

Deprecated: Function session_register() is deprecated in /data/18/1/57/42/1220042/user/1300782/htdocs/includes/cart_class.php on line 213

Warning: Cannot modify header information - headers already sent by (output started at /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php:51) in /data/18/1/57/42/1220042/user/1300782/htdocs/add_to_cart.php on line 60

Which do you need help with?

I was trying to figure out what all of those errors meant. Seems that once i replaced the depricated session_unregister and session_register by calling $_SESSION[‘cart’] all of my fatal errors are gone, so your help was huge, thank you. Now, it simply doesn’t add anything to the cart, and i’m not sure why. This is the current code:

[php]

<? ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); require "./includes/cart_class.php"; require_once "./includes/functions.inc.php"; session_start(); //unset($_SESSION['cart']; //exit(); $product_id = $_POST['pid']; $pcid = $_POST['pcid']; $product_name = $_POST['product_name']; $price = $_POST['price']; $qty = $_POST['qty']; $att_id = $_POST['att_id']; $att_value_id = $_POST['att_value_id']; $pop = $_POST['pop']; /// if value =1 then comes from popup product details page. $page_type = $_POST['page_type']; /// if value =1 then comes from category pages. $stoolH = $_POST['stoolheight']; $arr_att = array($att_id, $att_value_id); $sql="select product_weight from yp_product where product_id='$product_id'"; $pweight=getSingleResult($sql); if($_SESSION['cart']!=""){ $cart=$_SESSION['cart']; } if($qty==''){ if($min_qty!=""){ $qty=$min_qty; }else{ $qty=1; } } if($qty==0){ $_SESSION['sess_msg']= "QUANTITY CANNOT BE LESS THAN 1"; header("Location: product_detail.php?pid=$pid&pcid=$pcid"); exit(); } if(!$_SESSION['cart']){ $cart = new cart(); $_SESSION['cart']; } $item_object = new product($product_id , $product_name , $price, $arr_att, $qty , $pweight, $stoolH); $cart->addToCart($item_object , $product_id , $product_name , $price , $arr_att , $qty , $stoolH , $pweight); $back="modify_cart_frm.php"; header("Location: ".$back); exit; ?>

So what might be causing this to not add an item to the cart. As i look through the code it seems fine to me. the addtocart function was defined to look for the product id and add that to the item object, then look at the attributes of product id and add them as well.

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service