2016-04-26 58 views
0

我正在使用php創建電子商務網站。我現在已經在購物車上了。我的書數量沒有更新,它總是採用默認的「1」值。我想在「數量」字段中下拉列表,用戶最多隻能從我的網站購買3本書。產品數量未在我的電子商務購物車中更新

這裏是我的cart.php

<?php include_once("header.php");?> 
<div id="products_box"> 
<form action="" method="post" enctype="multipart/form-data"> 

<table align="center" width="700" bgcolor="#CCFFCC"> 
<br> 
<tr align="center"> 
    <td style="font-family:'Adobe Garamond Pro', 'Times New Roman'; font-size:20px" colspan="4"><h2> Your Book(s) Details </h2></td> 
</tr> 

<tr align="center" style="font-size:20px; font-family:'Adobe Garamond Pro', 'Times New Roman'"> 
<th>Remove</th> 
<th>Book(s)</th> 
<th>Quantity</th> 
<th>Total Price</th> 
</tr> 

<?php 
$total=0; 
global $con; 
$ip = getIp(); 
$sel_price = "select * from cart where ip_add='$ip'"; 
$run_price = mysqli_query($con, $sel_price) or die(mysqli_error($con)); 

    while($p_price = mysqli_fetch_array($run_price)) 
{ 
    $b_id = $p_price['cb_id']; 
    $b_price = "select * from books where b_id='$b_id'"; 
    $run_book_price = mysqli_query($con, $b_price) or die(mysqli_error($con)); 

    while($pp_price = mysqli_fetch_array($run_book_price)) 
    { 
    $b_price = array($pp_price['b_price']); 
    $b_title = $pp_price['b_title']; 
    $b_image = $pp_price['b_image']; 
    $single_price = $pp_price['b_price']; 

    $values = array_sum($b_price); 
    $total += $values; 

    if(isset($_POST['update_qty'])) 
{ 
    if(isset($_POST['qty' . '$b_id'])) 
    { 


    $quantity = (int)$_POST['qty' . '$b_id']; 
    $run_update = "update cart set qty='$qty' where cb_id='$b_id'"; 
    $run_qty = mysqli_query($con, $run_update) or die(mysqli_error($con)); 
    $total = $total*$qty; 

    } 
} 

?> 

<tr align="center"> 
<td><input type="checkbox" name="remove[]" value="<?php echo $b_id; ?>"/> </td> 
<td><h3><?php echo $b_title; ?></h3> 
<img src="admin_area/books_images/<?php echo $b_image; ?>" width="100" height="100" style="border: ridge"/> 
</td> 
<td><input type="text" name="qty<?php echo($b_id); ?>" size="8" value=" 
    <?php 
    $default_qty=1; 
    if(!isset($_POST['qty'])) 
    { 
    echo $default_qty; 
    } 
    ?>"/></td> 

<td> <?php echo "Rs.\n". $single_price; ?></td> 
</tr> 

<?php } }?> 

<tr align="right"> 
    <td colspan="4"><b> Sub Total: </b></td> 
    <td colspan="4"><?php echo "Rs.\n". $total; ?></td> 
    </tr> 


    <tr> 
    <td align="left" style="padding-left:12px"><input type="submit" name="remove_pro" value="Remove" style="background: #000000; font-family:'Adobe Garamond Pro Bold', 'Adobe Garamond Pro', 'Adobe Caslon Pro'; font-size:18px; color:#FFFFFF"/></td> 


    <td align="center" style="padding-right:20px"><input type="submit" name="continue" value="Continue Shopping" style="background: #000000; font-family:'Adobe Garamond Pro Bold', 'Adobe Garamond Pro', 'Adobe Caslon Pro'; font-size:18px; color:#FFFFFF" /></td> 


    <td align="center" style="padding-left:12px"><input type="submit" name="update_qty" value="Update Quantity" style="background: #000000; font-family:'Adobe Garamond Pro Bold', 'Adobe Garamond Pro', 'Adobe Caslon Pro'; font-size:18px; color:#FFFFFF"/></td> 


    <td align="right" colspan="4"><input type="submit" name="checkout" value="Checkout" style="background: #000000; font-family:'Adobe Garamond Pro Bold', 'Adobe Garamond Pro', 'Adobe Caslon Pro'; font-size:18px; color:#FFFFFF; margin-right:10px"/></td> 
    </tr> 

</table> 

</form> 


<?php 

    global $con; 
    $ip = getIp(); 

    if(isset($_POST['remove_pro'])) 
{ 
    foreach($_POST['remove'] as $remove_id) 
{ 
    $delete_pro = "delete from cart where cb_id='$remove_id' AND ip_add='$ip'"; 
    $run_query = mysqli_query($con, $delete_pro) or die(mysqli_error($con)); 
    if($run_query) 
    { 
    echo "<script>window.open('cart.php','_self')</script>"; 
    } 
} 
} 

if(isset($_POST['continue'])) 
{ 
echo "<script>window.open('index.php','_self')</script>"; 
} 

if(isset($_POST['checkout'])) 
{ 
echo "<script>window.open('checkout.php','_self')</script>"; 
} 


?> 

</div> 


</div> 



</div> 
<!--Content Wrapper Ends---> 


<?php include_once("footer.php");?> 
<!--Main Wrapper Ends--> 


</body> 

</html> 
+0

任何人都可以有解決以上問題 –

回答

0

你從來沒有定義$qty這是在您的查詢中使用:

$quantity = (int)$_POST['qty' . '$b_id']; 
$run_update = "update cart set qty='$qty' where cb_id='$b_id'"; 
$run_qty = mysqli_query($con, $run_update) or die(mysqli_error($con)); 
$total = $total*$qty; 

變化$quantity

$qty = (int)$_POST['qty' . '$b_id']; 
+0

okzz我會改變這個 –

+0

先生,我的第一個產品ct現在在購物車中工作的很好,但現在不變的第二件產品的數量也都是兩個產品相應的「總價」不變。 –

+0

將'$ total = $ total * $ qty;'更改爲'$ total = $ total +($ single_price * $ qty);'以獲得總價格。 – fislerdata

相關問題