我正在用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'];