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

    Join Date
    Apr 2013
    Rep Power

    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":

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

    case "empty":


    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 Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Rep Power
    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