Jump to content

Need help with this script


mshaynerush

Recommended Posts

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

?>

</code>

 

it gives a fatal error. Problem started a week or two ago, but the code hasn't changed to my knowledge. I did not write this.

Link to comment
Share on other sites

the error means exactly what it says, $cart is a non-object, but the code expects it to be one, an instance of your cart class.

 

the code is using two depreciated/removed functions - session_register() and session_is_registered(). however, if your php version was updated to the point where they don't exist at all, you would be getting a fatal runtime error at the first one of them and you would never get to line 50 in the file. for php versions (5.4 and higher) where these functions have been removed, the code will need to be rewritten to either just use the session variable directly to hold an instance of your cart class or you will need to copy the $cart variable out-of/in-to the session variable.

 

afaik, for previous php versions, session_register() 'worked', even with register_globals turned off, despite what the php documentation states (i tested this myself at one point in time), and the code should be registering the $cart variable as a session variable.

 

so it would appear that the session variable exists, but has been overwritten with something other than an instance of your cart class.

 

for some first steps at debugging, do the following -

 

1) set php's error_reporting to E_ALL and display_errors to ON, immediately after the first opening <?php tag on that page.

 

2) on line 49 in that file, use var_dump($cart); to see exactly what is in the $cart variable at that point.

Link to comment
Share on other sites

Thank you for that. this is the error i get after all that:

 

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

Link to comment
Share on other sites

the only thing i can tell you from that (besides that the code needs to be completely rewritten in the near future) is - something about the problem changed as $cart is now an object and the original error isn't being produced.

 

can you reproduce the conditions that led up to the error in the original post and show what the var_dump($cart); statement displays for that case?

Link to comment
Share on other sites

I have no way of doing that because I really don't know. Weeks ago, he was receiving orders and now he can't suddenly. I found some stuff about deprecated tags, changed out session_register for $_SESSION['cart]' and session_unregister with unset($_SESSION['cart']) and that removed the fatal errors. NOW all that happens is that the cart is empty. NO errors, but nothing is being added to the cart.

 

this is my most recent, fatal error free code:

<?

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				= $_REQUEST["pid"];
	$pcid					= $_REQUEST['pcid'];
	$product_name			= $_REQUEST['product_name'];
	$price					= $_REQUEST['price'];
	$qty					= $_REQUEST['qty'];
	$att_id					= $_REQUEST['att_id'];
	$att_value_id			= $_REQUEST['att_value_id'];
	$pop					= $_REQUEST["pop"];  /// if value =1 then comes from popup product details page.
	$page_type				= $_REQUEST['page_type'];  /// if value =1 then comes from category pages.
    $stoolH                 = $_REQUEST['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;

?>


Link to comment
Share on other sites

because $cart is no longer 'registered' as a being associated with a session variable, changes to it don't get saved back to $_SESSION['cart']. the most straight-froward fix would be to use $_SESSION['cart'] = $cart; after any statements that modify $cart.

 

edit: btw - you can replace those two bits of $_SESSION['cart'] related logic with just the following -

if(!isset($_SESSION['cart'])){
    $_SESSION['cart'] =  new cart();
}
    $cart=$_SESSION['cart'];
Edited by mac_gyver
Link to comment
Share on other sites

In the page, there was a drop down menu populated by state names that were then converted from actual state names to abbreviations. I moved that function from the actual page to the functions.inc.php file and then made a function call in place of where convert_state() function was. That fixed the problem. Boy what a pain this was. PHP was fine. I had been working on this site, mostly front end and some small PHP code here and there for years. It just now started doing all of this, but with your help here, I am proud and elated to announce that your help and Google, all errors are gone. Awesome! Thank you for your time and expertise in helping me out with this issue!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.