我需要单独更新数量,但是当我更新它时会更新所有数量

时间:2016-07-01 16:20:49

标签: php html mysqli sql-update cart

<tr align="center">
    <td>
        <input type="checkbox" name="remove[]" value="<?php echo $pro_id;?>">//removes checked items  
    </td>
    <td>
      <img src="admin_area/product_images/<?php echo $product_image?>" width="60" height="60"/><br>//displays products image
      <?php echo $product_title ?>//displays products title
  </td>
  <td>
      <input type="text" size="4" name="qty" value="<?php echo $_SESSION["qty"]; ?>"><span style="color:red">Required</span>
  </td>//textbox where user enters the new quantity
  <input type="hidden" name="pro_id" value="<?php echo $pro_id; ?>">
</td>
<?php
$sql = "select * from cart";
$run_query = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($run_query)) {
    $_SESSION["qty"] = $row['qty'];
}

if (isset($_POST['update_cart'])) {
    $ip = getIp();
    $Updateqty = $_POST['qty'];
    //get quantity from the text boxes which are repeated with every product

    $pro_id = $_POST['pro_id'];
    //gets the id from the hidden field

    $update_qty = "UPDATE cart SET qty='$Updateqty' where p_id='$pro_id' and ip_add='$ip'";
    $run_qty = mysqli_query($con, $update_qty);

    if ($run_qty) {
        echo"<script>window.open('cart.php','_self')</script>";
    }
}
?>
</tr>

1 个答案:

答案 0 :(得分:1)

这里有一个问题:

$sql = "select * from cart";
$run_query = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($run_query)) {
    $_SESSION["qty"] = $row['qty'];
}

此while循环将在每次迭代时覆盖$_SESSION["qty"],因此它将始终设置为购物车中最后一项的数量。

然后在每个项目的显示中使用该会话值。

<input type="text" size="4" name="qty" value="<?php echo $_SESSION["qty"]; ?>"><span style="color:red">Required</span>

此外,如果您在问题中显示的内容是来自包含多行的表格中的一行,并且您已将整个表格包装在一个表单中,则会提交每个输入,并且因为您有多个值对于qtypro_id,只会使用最后一个。这就是让每个数量都得到更新的原因。

我建议采用这样的方法来实现它。这可能不适合你,因为我猜到了一些名字,但应该足以表明我的想法:

<?php

if (isset($_POST['update_cart'])) {
    $ip = getIp();

    // prepare an update statement
    $sql = "UPDATE cart SET qty=? where p_id=? and ip_add=?";
    $stmt = mysqli_prepare($con, $sql);

    // loop over each quantity and update
    foreach ($_POST['qty'] as $pro_id => $qty) {
        $stmt->bind_param("iis", $qty, $pro_id, $ip);
        $stmt->execute();
    }
}

$sql = "select * from cart";
$run_query = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($run_query)) {
    // update the session with the new values
    $_SESSION[$row['p_id']]['qty'] = $row['qty'];
}

?>

这应该更新每个项目的数量,但这取决于您的表单提供的qty值数组。为此,您需要使用如下数组语法命名输入:

<form action="" method="post">
    <table>
        <?php foreach ($products as $product): ?>
        <tr>
            <td>Other product info</td>
            <td>
                <input type="text" 
                    name="qty[<?php echo $product['id'] ?>]"
                    value="<?php echo $_SESSION[$product['id']]['qty']; ?>">
            </td>
        </tr>            
        <?php endforeach ?>
    </table>
    <input type="submit" name="update_cart" value="Update Cart">
</form>