【问题标题】:php cart out of stock mysqlphp 购物车缺货 mysql
【发布时间】:2016-03-10 04:52:33
【问题描述】:

以下 PHP 验证代码有效,但是如果我返回产品页面添加更多相同的产品,当数量不可用时,它可以覆盖购物车页面上的 out'

下面显示了一个链接到 $cart 会话数据的 if 语句以及存储在表中的 $film 数量。

由于某种原因,$film['quantity'] 的值为 1,所以如果数量 >1 添加到购物车 'out' 显示哪个是错误的?

    <?php $itemcount = $_SESSION['itemcount'];


                if($cart[QUANTITY][$i] > $table['quantity'] ) { ?>
      <span  style="font-family:'arial';">Out </span>
      <?php } ?>

【问题讨论】:

  • 我认为如果某些机构对您的要求超出了可用的范围,您需要显示可用的产品吗?
  • 我已经完成了我有一个产品页面和购物车页面。我正在尝试添加验证,因此客户无法添加更多产品然后有库存。

标签: php mysql


【解决方案1】:

显示的代码很难说是什么问题。但是这里有一个很好的嫌疑人:

if($cart[QUANTITY][$i] > $film['quantity'] ) {

我怀疑应该是:

if($cart[$i]['QUANTITY'] > $film['quantity'] ) {

其他可能有问题的地方:

  1. 你有session_start()ed 吗?
  2. 是密钥QUANTITY 不是quantity(大小写很重要)
  3. 您是否碰巧在某处定义了常量QUANTITY? (引用很重要)

【讨论】:

  • 我一开始用的 session_start() 数量和数量不一样。数量正在存储购物车的会话。数量是从数据库表 片子哪家店有多少有现货? $film['quantity'] 显示不正确,这是我的问题
【解决方案2】:

我认为它会帮助你:

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

【讨论】:

  • 上述 if 语句是在 product 'out' 时通过 div 显示错误信息。我已经有一个添加到购物车功能,所以当我添加你的代码时,它会导致错误 Undefined index: product_id in , Undefined index: in,Undefined variable: product_count in 等。我应该给你发送我的添加到购物车功能,因为我认为我的问题是,当将相同的产品添加到购物车时,它不会更新数量。就像下面的链接。 stackoverflow.com/questions/22716235/…
  • 请在产品页面上显示“添加到购物车”功能和“产品展示”功能
猜你喜欢
  • 2012-04-10
  • 2014-06-11
  • 2015-06-09
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2019-09-01
  • 2011-11-27
  • 1970-01-01
相关资源
最近更新 更多