Search code examples
phpmysqlshopping-cart

PHP shopping cart gives "Notice: Undefined index" error


I am trying to create a shopping cart. After adding the product to the cart the output is something like

Gold ring Notice: Undefined index: quantity in C:\xampp\htdocs\shopping\index.php on line 37

It displays the name of the product. But doesn't give the number of items. The codes are as follows

Index.php

<?php
    session_start();
    require_once("includes/connect.php");

    if(isset($_GET['page'])){
        $pages=array('products','cart');
        if(in_array($_GET['page'], $pages)){
            $page=$_GET['page'];
        } else{
            $page='products';
        }
    } else{
        $page='products';
    }
?>

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/reset.css">
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <title>Simple Shopping Cart</title>
    </head>
    <body>
        <div id="container">
            <div id="main"><?php require($page . ".php"); ?></div>
            <div id="sideNav"><h1>Your Cart:</h1>
                <?php 
                    if(isset($_SESSION['cart'])){
                        $sql_cart = "SELECT * FROM products WHERE product_id IN (";
                        foreach ($_SESSION['cart'] as $key => $value) {
                            $sql_cart .= $key .",";
                        }
                        $sql_cart = substr($sql_cart, 0, -1) . ")";
                        $get_cart = mysql_query($sql_cart);

                        while($show_cart = mysql_fetch_array($get_cart)){ ?>
                            <p><?php echo $show_cart['name']; echo " " . $_SESSION['cart'][$show_cart['product_id']]['quantity']; ?></p>
                            <a href="index.php?page=cart">Goto Cart</a>
                        <?php }
                    } else {
                        echo "Please add an item to cart";
                    }
                ?>
            </div>
        </div>
    </body>
</html>

products.php

<?php

    if(isset($_GET['action']) && $_GET['action'] == "add"){
        $id=intval($_GET['id']);
        if(isset($_SESSION['cart'][$id])){
            $_SESSION['cart'][$id]['session']++;
        } else{
            $get_product = "SELECT * from products where product_id={$id}";
            $query_product = mysql_query($get_product);

            if(mysql_num_rows($query_product) != 0){
                $detail = mysql_fetch_array($query_product);
                $_SESSION['cart'][$detail['product_id']] = array('quantity' => 1, 'price' => $detail['price']);
            } else{
                $message = "Invalid product id";
            }
        }
    }


?>
<h2 class="Error"><?php if(isset($message)){ echo $message; } ?></h2>
<h1>Products</h1>
<?php

$query=mysql_query("select * from products");
?>
<?php
while ($row = mysql_fetch_assoc($query)) { 
    echo '<div id="products"><span>' . $row['name'] . "</span><span>" . $row['description'] . "</span><span>&pound" 
    . $row['price'] . "</span><span>" ?><a href="index.php?page=products&action=add&id=<?php echo $row['product_id']; ?>">Add to cart</a></span></div><?php
}
?>

Can anyone help me with this? Thanks!!!

Update On clicking add to cart, the product is added to the cart and displayed in the cart. But if I try to add the same product again it gives the error. (i.e.) The quantity of the product is not increased.


Solution

  • <p><?php echo $show_cart['name']; echo " " . $_SESSION['cart'][$show_cart['product_id']]['quantity']; ?></p>
    

    Whats ['quantity'] ? It should be like, $product['quantity'].