我正在尝试更新basket.php页面中的产品数量。该数量还将发布在 MySQL 数据库的 basket 表中的 qty 列下。但不幸的是它并没有像我希望的那样工作。
问题:
- 更新数量仅适用于购物篮中的一件商品。
- 在框中输入数量“3”并单击 update_qty 按钮后,框中的值将更改回 1。但价格已正确更新。
- 添加多个商品并更新其数量将导致总价错误,并且最后一个商品的数量将保存在购物篮表中所有商品的数量列中。
数据库结构:
篮子 table
f_id type int(10) primary key//food id
ip_add type varchar(225)
qty type int(10)//quantity in basket
食物表
1 food_id type int(100) AUTO_INCREMENT primary key
2 food_cat type int(100)
3 food_type type int(100)
4 food_title type varchar(255)
5 food_price type float(23,2)
6 food_desc type text
7 food_image type text
8 food_keywords type text
这是我的代码:
篮子.php
session_start();
include ("functions/functions.php");
<form action = " " method="post" enctype="multipart/form-data">
<table>
<tr>
<th>Remove</th>
<th>Dish(s)</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<?php
$total = 0;
global $con;
$ip = getIp();
$sel_price = "select * from basket where ip_add='$ip'";
$run_price = mysqli_query($con, $sel_price);
while ($p_price = mysqli_fetch_array($run_price)) {
$foo_id = $p_price['f_id'];
$foo_price = "select * from food where food_id ='$foo_id'";
$run_foo_price = mysqli_query($con, $foo_price);
while ($ff_price = mysqli_fetch_array($run_foo_price)) {
$food_price = array ($ff_price['food_price']);
$food_title = $ff_price['food_title'];
$food_image = $ff_price ['food_image'];
$single_price = $ff_price['food_price'];
$values = array_sum($food_price);
$total += $values;
?>
<?php
//updates quantity of items in basket
if (isset($_POST['update_qty'])){
$qty = $_POST['qty'];
$update_qty = "update basket set qty='$qty'";
$run_qty = mysqli_query($con, $update_qty);
$total = $values*$qty;
}
?>
<input type="number" name="qty" value="<?php echo $_SESSION['qty']; ?>" >
<?php } } ?>
<b>Sub Total:</b>
<?php echo "£". $total;?>
<div id="up">
<input type="submit" name="update_basket" value="Remove">
</div>
<div id="up">
<input type="submit" name="update_qty" value="Update Quantity">
</div>
<div id="con">
<input type="submit" name="continue" value="Continue Shopping">
</div>
<div id="chck">
<a href="checkout.php"><button type="hidden">Checkout</button></a>
</div>
functions.php
//Creates the basket
function basket(){
if (isset($_GET['add_basket'])){
global $con;
$ip = getIp();
$foo_id = $_GET['add_basket'];
$check_foo = "select * from basket where ip_add= '$ip' AND f_id =
'$foo_id'";
$run_check = mysqli_query($con, $check_foo);
if(mysqli_num_rows($run_check)>0){
echo " ";
}
else {
$insert_foo = "insert into basket (f_id,ip_add) values ('$foo_id', '$ip')";
$run_foo = mysqli_query($con, $insert_foo);
echo "<script>window.open('order.php','_self')</script>";
}
}
}
//Get total items in Basket
function total_items(){
if (isset ($_GET['add_basket'])){
global $con;
$ip = getIp();
$get_items = "select * from basket where ip_add='$ip'";
$run_items = mysqli_query($con, $get_items);
$count_items = mysqli_num_rows($run_items);
}
else {
global $con;
$ip = getIp();
$get_items = "select * from basket where ip_add='$ip'";
$run_items = mysqli_query($con, $get_items);
$count_items = mysqli_num_rows($run_items);
}
echo $count_items;
}
//get the total price of items in basket
function total_price(){
$total = 0;
global $con;
$ip = getIp();
$sel_price = "select * from basket where ip_add='$ip'";
$run_price = mysqli_query($con, $sel_price);
while ($p_price = mysqli_fetch_array($run_price)) {
$foo_id = $p_price['f_id'];
$foo_price = "select * from food where food_id ='$foo_id'";
$run_foo_price = mysqli_query($con, $foo_price);
while ($ff_price = mysqli_fetch_array($run_foo_price)) {
$food_price = array ($ff_price['food_price']);
$values = array_sum($food_price);
$total += $values;
}
}
echo "£" . $total;
}
我如何更改代码来解决这些问题。
谢谢。
最佳答案
@connor991,你的代码非常笨拙。你需要重写你的代码。我会建议两种方法。
但首先您需要有一个表单,其中显示所有商品并让用户选择要购买的商品。然后您将他们重定向到您提供更多信息的地方,然后他们选择质量。
然后,第一种方法:显示购物车中的所有商品时,将它们显示在单独的 form
标记中。这样,您就有了一个包含当前数量
的文本框
,用户可以根据需要进行更新。您还提供了一个submit
按钮来提交表单以进行更新。这样,您就可以获取单独的 food_id
和 qty
并进行相应更新。
我还意识到,由于一个用户ip
可以购买多件商品,因此仅根据ip_add
更新将更新他/她的ip_add
下的所有产品。因此,您需要这样做:
// Assuming that you have ip and food_id values already
UPDATE basket SET qty=$qty WHERE ip_add=$ip AND food_id=$food_id
第二个选项是显示表格中的所有项目,而不提供textbook
来立即更新qty
。您针对每个购物车项目显示一个 update
链接,以便当他们单击该链接时,您可以将该特定购物车项目的 food_id 和 ip_id 传递到新页面,您可以在该页面中从数据库获取数据并立即显示它们加上qty
,但使用教科书
中的qty
。当用户单击此页面上的更新按钮时,您将从该特定页面检索值并执行更新。
希望这有帮助。
关于php - 更新产品数量(php和Mysql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28530367/