我也无法更新购物车和数据库表中的商品数量。有没有错误?我没有使用会话记录添加到购物车中的商品。对不起,我不擅长编码。为我的最后一年项目做这件事。谢谢!
<form action="" method="post" enctype="multipart/form-data">
<table width="100%" cellpadding="6" cellspacing="0">
<thead>
<tr height="32">
<th width="10%">QTY</th>
<th width="37%">ITEM(S)</th>
<th width="18%">PRICE</th>
<th width="19%">TOTAL</th>
<th width="16%">REMOVE</th>
</tr>
</thead>
<tbody>
<?php
$total = 0; //set initial total value
global $con;
$ip = getIp();
$sel_price = "select * from cart where ip_add = '$ip'";
$run_price = mysqli_query($con, $sel_price);
while($p_price = mysqli_fetch_array($run_price)){
$qty = $p_price['qty'];
$cart_id = $p_price['cart_id'];
$pac_id = $p_price['p_id'];
$pac_price = "select * from package where package_id = '$pac_id'";
$run_pac_price = mysqli_query($con, $pac_price);
while($pp_price = mysqli_fetch_array($run_pac_price)){
//set variables to use in content below
$package_title = $pp_price['package_title'];
$package_image = $pp_price['package_image'];
$package_price = array($pp_price['package_price']);
$single_price = $pp_price['package_price'];
$subtotal = ($single_price * $qty); //calculate Price x Qty
$total = ($total + $subtotal); //add subtotal to total var
?>
<tr class="'.$bg_color.'">
<td><input type="text" size="2" maxlength="2" name="qty" value="<?php echo $_SESSION['qty'];?>" />
</td>
//update quantity part
<?php
if(isset($_POST['update_cart'])){
$qty = $_POST['qty'];
$update_qty = "update cart set qty='$qty' where cart_id='$cart_id'";
$run_qty = mysqli_query($con, $update_qty);
$_SESSION['qty']=$qty;
$subtotal = ($single_price * $_SESSION['qty']);
}
?>
<td><br><?php echo $package_title; ?><br>
<img src="admin_area/product_images/<?php echo $package_image; ?>" width="80" height="80"/><br></td>
<td><?php echo "RM".$single_price; ?></td>
<td><?php echo "RM".$subtotal; ?></td>
<td><a href="removefromcart.php?delete_cart=<?php echo $cart_id ?>"><img src ="admin_area/img/icons/delete.png"/></a></td>
</tr>
<?php }} ?>
<tr>
<td colspan="5">
<span style="float:right;text-align: right;">Amount Payable : <?php echo "RM" .sprintf("%01.2f", $total);?>
</span>
</td>
</tr>
<tr>
<td colspan="5">
<a href="checkout.php" class="button">Checkout</a>
<a href="funeral_package.php" class="button">Add More Items</a>
<button type="submit" name="update_cart">Update</button>
</td>
</tr>
</tbody>
</table>
</form>
答案 0 :(得分:0)
我在这里使用$ PreV_qty作为变量。这意味着你必须获得之前的数量,然后更新
if(isset($_POST['update_cart'])){
$PreV_qty = $_SESSION['qty'];
$qty = $PreV_qty+$_POST['qty'];
$update_qty = "update cart set qty='$qty' where cart_id='$cart_id'";
$run_qty = mysqli_query($con, $update_qty);
$_SESSION['qty']=$qty;
$subtotal = ($single_price * $_SESSION['qty']);
}
答案 1 :(得分:0)
尝试将以下部分移到脚本顶部
if(isset($_POST['update_cart'])){
$qty = $_POST['qty'];
$update_qty = "update cart set qty='$qty' where cart_id='$cart_id'";
$run_qty = mysqli_query($con, $update_qty);
$_SESSION['qty']=$qty;
$subtotal = ($single_price * $_SESSION['qty']);
}
或以上<form action="" method="post" enctype="multipart/form-data">