0

My script was working perfectly fine for ages then this error cropped up saying I have an undefined index that won't allow me to add products to my shopping cart.

PHP: Notice 'undefined index: product_name in C:\xampp\htdocs\Autoparts Warehouse2\grilles.php on line 243

if(isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"])>0) { 
    echo ' ';
    echo ' Your Shopping Cart '; 
    echo ' '; 
    echo ''; 
    echo ''; 
    $total =0; 
    $b = 0;
    foreach ($_SESSION["cart_products"] as $cart_itm) 
        print_r($cart_itm); {
        $product_name  = $cart_itm["product_name"];
        $product_qty   = $cart_itm["product_qty"]; 
        $product_price = $cart_itm["product_price"];
        $product_code  = $cart_itm["product_code"];
        $product_color = $cart_itm["product_color"];

I have tried using $_POST and $_GET also but to neither have worked any help would be greatly appreciated. Thanks.

m.corr
  • 1
  • 1

1 Answers1

0

Did you add session_start() at the top of your code?

session_start();
if(isset($_SESSION["cart_products"]) ) {
    if( count($_SESSION["cart_products"]) > 0 ){
        echo ' '; 
        echo ' Your Shopping Cart '; 
        echo ' '; 
        echo ''; 
        echo ''; 
        $total =0; 
        $b = 0; 
        foreach ( $_SESSION["cart_products"] as $cart_itm ) { 
            $product_name   = $cart_itm["product_name"]; 
            $product_qty    = $cart_itm["product_qty"]; 
            $product_price  = $cart_itm["product_price"]; 
            $product_code   = $cart_itm["product_code"]; 
            $product_color  = $cart_itm["product_color"];
        }
    }
}

You can try like this. Because if $_SESSION["cart_products"] not set then count($_SESSION["cart_products"]) is also not set. Thanks.

  • have edited my code accordingly but unfortunately still getting the same error returned that there is an undefined index: product_name – m.corr May 28 '18 at 17:29
  • `$product_name = isset( $cart_itm["product_name"] ) ? $cart_itm["product_name"] : '';` try this, I think you didn't set `$cart_itm["product_name"]`. Thanks. – pranay kantey sarker May 28 '18 at 18:53