PHP购物车给出“通知:未定义的索引”错误

时间:2015-04-12 22:35:49

标签: php mysql shopping-cart

我正在尝试创建购物车。将产品添加到购物车后,输出类似于

  

金戒指   注意:未定义的索引:第37行的C:\ xampp \ htdocs \ shopping \ index.php中的数量

显示产品名称。但是没有给出物品的数量。代码如下

的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
}
?>

任何人都可以帮我吗?感谢!!!

更新 点击添加到购物车后,产品将添加到购物车并显示在购物车中。但是,如果我再次尝试添加相同的产品,则会出错。 (即)产品的数量没有增加。

1 个答案:

答案 0 :(得分:0)

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

什么是[&#39;数量&#39;]?它应该是,$ product [&#39;数量&#39;]。