【发布时间】:2015-04-16 07:28:00
【问题描述】:
我正在尝试更新 basket.php 页面中的产品数量。该数量也将发布在 MySQL 数据库中 basket 表中的 qty 列下。但不幸的是,它并没有像我希望的那样工作。
问题:
- 更新数量仅适用于购物篮中的一件商品。
- 在框内输入数量说“3”并单击update_qty按钮后,框内的值变回1。但价格已正确更新。
- 添加多个商品并更新其数量会导致总价错误,并且最后一个商品的数量会保存在购物篮表中所有商品的数量列中。
数据库结构:
篮子桌
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
这是我的代码:
basket.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;
}
如何更改我的代码来解决这些问题。
谢谢。
【问题讨论】:
-
这里有一些不好的做法,不要使用IP来识别人。一个IP可以上千人,任何一个都可以使用多个IP
-
谢谢,我稍后会更改。目前我正在尝试让核心功能正常工作
-
识别合适的购物车\用户不是核心?
-
从未说过不是。我正在尝试一次解决一个问题
标签: php html mysql session phpmyadmin