PHP 购物车出现 "Notice: Undefined index" 错误
PHP shopping cart gives "Notice: Undefined index" error
我正在尝试创建购物车。将产品添加到购物车后,输出类似于
Gold ring
Notice: Undefined index: quantity in C:\xampp\htdocs\shopping\index.php on line 37
显示产品名称。但没有给出项目的数量。代码如下
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>£"
. $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
}
?>
谁能帮我解决这个问题?谢谢!!!
更新
单击添加到购物车后,产品将添加到购物车并显示在购物车中。但是,如果我尝试再次添加相同的产品,则会出现错误。 (即)产品的数量没有增加。
<p><?php echo $show_cart['name']; echo " " . $_SESSION['cart'][$show_cart['product_id']]['quantity']; ?></p>
什么是 ['quantity']?它应该像 $product['quantity'].
我正在尝试创建购物车。将产品添加到购物车后,输出类似于
Gold ring Notice: Undefined index: quantity in C:\xampp\htdocs\shopping\index.php on line 37
显示产品名称。但没有给出项目的数量。代码如下
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>£"
. $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
}
?>
谁能帮我解决这个问题?谢谢!!!
更新 单击添加到购物车后,产品将添加到购物车并显示在购物车中。但是,如果我尝试再次添加相同的产品,则会出现错误。 (即)产品的数量没有增加。
<p><?php echo $show_cart['name']; echo " " . $_SESSION['cart'][$show_cart['product_id']]['quantity']; ?></p>
什么是 ['quantity']?它应该像 $product['quantity'].