[英]Get quantity of certain product from database instead of session - Shopping Cart (PHP & MySQL)
I have been trying to create a persistent MySQL-based shopping cart application starting from a session-based php shopping cart.我一直在尝试创建一个持久的基于 MySQL 的购物车应用程序,从一个基于会话的 php 购物车开始。 The
cart
table has the structure userid | productid | quantity | postingdate
cart
表具有结构userid | productid | quantity | postingdate
userid | productid | quantity | postingdate
userid | productid | quantity | postingdate
. userid | productid | quantity | postingdate
。 In the session-based implementation, the quantity info for a certain product was stored in $_SESSION['cart'][$row['id']]['quantity'];
在基于会话的实现中,特定产品的数量信息存储在
$_SESSION['cart'][$row['id']]['quantity'];
; ; I am facing trouble with retrieving the quantity info for a certain product from the
cart
table in the database because I can't seem to find how to extract the productId
necessary for the $getquantity
query.我在从数据库的
cart
表中检索特定产品的数量信息时遇到了麻烦,因为我似乎无法找到如何提取$getquantity
查询所需的productId
。
$sql1 = "SELECT * FROM cart WHERE userid='$userId'"; //selects the user's cart
$query1=mysqli_query($con,$sql1);
if (!empty($query1)) { //if the user's cart isn't empty
while($row = mysqli_fetch_array($query1)){
$query = mysqli_query($con,"SELECT * FROM products JOIN cart ON cart.productid=products.id
ORDER BY cart.postingDate DESC"); //selects all the products in the current user's cart
}
$totalprice=0;
$totalqunty=0;
if (!empty($query)) {
while ($row = mysqli_fetch_array($query)) { //for each product
//$quantity=$_SESSION['cart'][$row['id']]['quantity']; //SESSION IMPLEMENTATION
$id = $row['id'];
$quantity = mysqli_query($con,"SELECT quantity FROM cart WHERE userid='$userId' AND productid='$id'"); //MY IMPLEMENTATION, THE WRONG PART IS WITH $id
$subtotal= $quantity*$row['productPrice']+$row['shippingCharge'];
$totalprice += $subtotal;
$_SESSION['qnty']=$totalqunty+=$quantity; //SESSION IMPLEMENTATION
}
}
}
I've tried $row['id']
but it returns an array of all the productid
from the records returned by $query
and MySQl doesn't seem to accept it, while instead I need the $getquantity
to run on each productid
.我试过
$row['id']
但它从$query
返回的记录中返回所有productid
的数组,而 MySQl 似乎不接受它,而我需要$getquantity
在每个productid
上运行。 Please don't mind how it's vulnerable to SQL injections as I will get to that in later stages.请不要介意它如何容易受到 SQL 注入的攻击,因为我将在后面的阶段进行介绍。
Here's a working solution: I could have just selected the cart.quantity
attribute (considering how the products
and cart
table were joined):这是一个有效的解决方案:我可以选择
cart.quantity
属性(考虑products
和cart
表的连接方式):
$sql1 = "SELECT * FROM cart WHERE userid='$userId'"; //selects the user's cart
$query1=mysqli_query($con,$sql1);
if (!empty($query1)) { //if the user's cart isn't empty
while($row = mysqli_fetch_array($query1)){
$query = mysqli_query($con,"SELECT products.productImage1 , products.productName ,
products.id, cart.productId, cart.quantity as qty, products.productPrice,
products.shippingCharge FROM products JOIN cart ON cart.productid=products.id
ORDER BY cart.postingDate DESC"); //selects all the products in the current user's cart
}
$totalprice=0;
$totalqunty=0;
if (!empty($query)) {
while ($row = mysqli_fetch_array($query)) { //for each product
$quantity=$row['qty'];
$subtotal= $quantity*$row['productPrice']+$row['shippingCharge'];
$totalprice += $subtotal;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.