以下PHP验证代码有效,但是如果我返回产品页面以添加更多相同的产品,它可以在数量不可用时覆盖购物车页面上的"说出来"
下面显示了一个 if 语句,该语句链接到$cart会话数据以及存储在表中$film数量。
出于某种原因$film["数量"]的值为1,那么如果数量>1添加到购物车"出"显示哪个是错误的?
<?php $itemcount = $_SESSION['itemcount'];
if($cart[QUANTITY][$i] > $table['quantity'] ) { ?>
<span style="font-family:'arial';">Out </span>
<?php } ?>
显示代码后,很难说出问题是什么。然而,这里有一个很好的嫌疑人:
if($cart[QUANTITY][$i] > $film['quantity'] ) {
我怀疑应该是:
if($cart[$i]['QUANTITY'] > $film['quantity'] ) {
其他可能出错的地方:
- 你
session_start()
过吗? - 关键
QUANTITY
不quantity
(案例很重要) - 您是否碰巧在某处定义了常量
QUANTITY
?(引号很重要)
我认为它会帮助你:
<?php
echo $product_image;
echo $product_count;
echo $product_price;
echo "<form method='post' action=''>";
echo "<input type='text' name='product_shopping_count' />";
echo "<input type='hidden' name='product_id' value='".$product_id."' />";
echo "<input type='submit' name='addCart' value='AddCart' />";
echo "</form>";
if(isset($_POST['addCart']){
if(isset($_SESSION['cart']){
$cart = $_SESSION['cart'];
if($cart[$_POST['product_id']]['quantity']>$product_count){
echo "<span style='font-family:arial;font-size:16px;'>Out </span>";
}else if($_POST['product_shopping_count']>$product_count){
echo "<span style='font-family:arial;font-size:16px;'>Out </span>";
}else {
$cart['product_id']= $_POST['product_id'];
$cart['product_id']['quantity'] = $_POST['product_shopping_count'];
$cart['product_id']['price'] = $product_price;
$_SESSION['cart'] = $cart;
}
}
}
?>