#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Help with Notice: Undefined index


    Hey, Ive been having some trouble with my shopping cart. Everying works but im getting these annoying notices
    Notice: Undefined index: cart in C:\xampp\htdocs\work\example\cart.php on line 36

    Notice: Undefined offset: 2501 in C:\xampp\htdocs\work\example\cart.php on line 36


    2501 is the id number from one of the products
    I've been reading around with little luck on how to fix the problem
    Relevant code on the products page:

    <a href=\"cart.php?action=add&id=$id\">Add To Cart</a></td>

    Relevant code on Cart page.

    $product_id = $_GET['id'];
    $action = $_GET['action'];

    if($product_id && !productExists($product_id)) {
    die("Error. Product Doesn't Exist");
    }

    switch($action) {

    case "add":
    $_SESSION['cart'][$product_id]++;
    break;

    case "remove":
    $_SESSION['cart'][$product_id]--; //LINE 36!!
    if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]);
    break;

    case "empty":
    unset($_SESSION['cart']);
    break;

    }


    Thank you for your help
    Last edited by ultorian; April 24th, 2013 at 05:18 AM. Reason: forgot to add something
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,501
    Rep Power
    594
    That means that $_SESSION['cart'][$product_id] does not exist. If that is not an error, then test it with 'isset' before trying to use it.

    P.S. Please enclose you code in [ PHP ] tags. See the sticky at the top of this forum that says READ THIS BEFORE POSTING.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo