2015-10-28 35 views
0

我無法在我的購物車和數據庫表中更新我的物料數量。有沒有錯誤?我沒有使用會話來記錄添加到購物車中的物品。對不起,我不擅長編碼。爲我的最後一年項目做這個。謝謝!無法更新數量,保持顯示數量值'1'

<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

嘗試添加或死亡(mysqli_error());在查詢的執行之後(在mysqli_query()的後面)來查看你得到的錯誤。 –

+0

update cart set qty = qty +'$ qty'其中cart_id ='$ cart_id' – ManiMuthuPandi

回答

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']); 
} 
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">