image of the cart (sorry it's in Frensh)
我已经使用会话设置了购物车,但是现在我正在更新代码以将其添加到数据库中,但是当数量变化时我在更新时遇到问题,我不知道问题出在哪里。
添加和删除工作完美,但是当我增加数量时,它在数据库中始终设置为零,总数保持不变。
这是三个按钮的代码:删除-添加-删除
<td><a class='btn btn-warning' href='../Ressources/cart.php?remove=
{$row['product_id']}'><span class='glyphicon glyphicon-minus'></span></a>
<a class='btn btn-success' href='../Ressources/cart.php?add=
{$row['product_id']}'><span class='glyphicon glyphicon-plus'></span></a>
<a class='btn btn-danger' href='../Ressources/cart.php?delete=
{$row['product_id']}'><span class='glyphicon glyphicon-remove'></span></a>
</td>
这是PHP代码:
/*****************************add**********************/
if (isset($_GET['add'])) {
$id = $_SESSION['id_user'];
$idp = $_GET['add'];
$query = query("SELECT * FROM products WHERE product_id=".escape_string($_GET['add']). " ");
confirm($query);
while ($row = fetch_array($query)) {
if ($row['product_quantity'] != $_SESSION['product_' . $_GET['add']]) {
$_SESSION['product_' . $_GET['add']] += 1;
$quantity = $_SESSION['product_' . $_GET['add']];
$total = $quantity * $row['product_price'];
$query2 = query("SELECT * FROM cart where id_product=$idp and id_user=$id");
confirm($query2);
if (mysqli_num_rows($query2) == 0) {
$query1 = query("INSERT into cart values ($id,$idp,$quantity,$total)");
} else {
$query1 = query("UPDATE cart set quantity=$quantity and total=$total where id_product=$idp and id_user=$id");
}
confirm($query1);
redirect("../Public/checkout.php");
} else {
set_message("there's only " . $row['product_quantity'] . "
" . "available");
redirect("../Public/checkout.php");
}
}
}
/*************** remove *********************/
if (isset($_GET['remove'])) {
$idp = $_GET['remove'];
$id = $_SESSION['id_user'];
$_SESSION['product_' . $_GET['remove']]--;
$quantity = $_SESSION['product_' . $_GET['remove']];
$total = $_SESSION['item_total'];
$query = query("UPDATE cart set quantity=$quantity and total=$total
where id_product=$idp and id_user=$id");
confirm($query);
if ($_SESSION['product_' . $_GET['remove']] < 1) {
unset($_SESSION['item_total']);
unset($_SESSION['item_quantity']);
unset($_SESSION['nb_product']);
$query1 = query("DELETE FROM cart where id_product=$idp and id_user=$id");
confirm($query1);
redirect("../Public/checkout.php");
} else {
redirect("../Public/checkout.php");
}
}
/*************** delete ********************/
if (isset($_GET['delete'])) {
$idp = $_GET['delete'];
$id = $_SESSION['id_user'];
$_SESSION['product_' . $_GET['delete']] = '0';
unset($_SESSION['item_total']);
unset($_SESSION['item_quantity']);
unset($_SESSION['nb_produit']);
$query1 = query("DELETE FROM cart where id_product=$idp and id_user=$id");
confirm($query1);
redirect("../Public/checkout.php");
}
代码中仍然有一个功能来显示购物车中的产品(在表格中),但我需要先解决此问题)
我希望会话期间将数量和总计的值存储在数据库中,我们将不胜感激
答案 0 :(得分:-1)
我的更新查询语法有误,现在可以正常使用