【问题标题】:Update option is not working in Shopping cart in PHP更新选项在 PHP 的购物车中不起作用
【发布时间】:2015-12-23 09:08:53
【问题描述】:

我在 PHP 中创建了一个购物车项目,但在更新购物车商品数量的情况下,它无法按预期工作。最后添加的项目数量只能更新。我的显示项目和更新代码在这里:

<HTML>
<BODY>
<?php
session_start();
include 'connect.php';
if(isset($_POST['update'])) {
$p=$_POST['pname'];
$q=$_POST['quantity'];
        $querym =mysql_query("SELECT * FROM product WHERE product_name='$p'"); 
          while ($productByCode = mysql_fetch_array($querym)) {
                        $itemArray = array($productByCode["product_name"]=>array('name'=>$productByCode["product_name"],'p_id'=>$productByCode["p_id"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode["amount"]));
                        if(!empty($_SESSION["cart_item"])) {
            if(in_array($productByCode["product_name"],$_SESSION["cart_item"])) {
                foreach($_SESSION["cart_item"] as $k => $v) {
                        if($productByCode["product_name"] == $k)
                            $_SESSION["cart_item"][$k]["quantity"] =$q;
                    }
            } 
            else {
                $_SESSION["cart_item"] =    array_merge($_SESSION["cart_item"],$itemArray);

            }
             }      
            else 
            {

            $_SESSION["cart_item"] = $itemArray;
                        }
        }
                }

if(isset($_SESSION["cart_item"])){
$item_total = 0;
   ?>   
<form method="POST">        
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th><strong>Total</strong></th>
<th><strong>Action</strong></th>
</tr>   
<?php 
  foreach ($_SESSION["cart_item"] as $item){
  $total =($item["price"]*$item["quantity"]);

    ?>      
            <tr>
            <td><strong><?php echo $item["name"]; ?></strong></td>
            <td><input type="text" name="quantity" id="<?php echo     $item["p_id"];?>" class="quant" value="<?php echo $item["quantity"]; ?>" style="width:35;"><br>
            <input type="hidden" name="pname" value="<?php echo $item["name"]; ?>">
            <input type="hidden" name="p_id" value="<?php echo $item["p_id"]; ?>">
            <p><input type="submit" value="save" name="update"></p>
                <td align=right><?php echo "$".$item["price"]; ?></td>
                <td><?php echo "$".$total; ?></td>
            <td><a href="checkout.php?act=remove&p_name=<?php echo  $item["name"]; ?>" class="btnRemoveAction">Remove Item</a></td>
            </tr>
            <?php
    $item_total += ($item["price"]*$item["quantity"]);
   $_SESSION['tot']=$item_total ;
    }
    ?>
  <tr>
<td colspan="5" align=right><strong>Total:</strong><?php echo  "$".$item_total; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
 </BODY>
 </HTML>

提前致谢。

【问题讨论】:

  • 如果有这样的 SQL,请告诉我网上商店,这样我就可以避免它 - 谢谢。

标签: php mysql


【解决方案1】:

是您的$_SESSION["cart_item"][$k]["quantity"] 没有更新吗?

一个建议。你真的应该考虑防止 SQL 注入,https://en.wikipedia.org/wiki/SQL_injection

【讨论】:

  • 是的。只有最后一个产品数量只是更新。其他没有效果..更新的明智按钮只有在单击第一个产品数量字段时才有效。请帮助我。
  • 更多数据会更好 :-) 尝试在 if(isset($_SESSION["cart_item"])){ 之前添加 print '&lt;pre&gt;; print_r ($_SESSION); print '&lt;pre&gt;;,看看它在您测试购物车系统时如何更新。
猜你喜欢
  • 1970-01-01
  • 2017-02-16
  • 1970-01-01
  • 2014-05-05
  • 1970-01-01
  • 2016-08-01
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多