我一直在尝试从基于会话的php购物车开始创建一个基于MySQL的持久购物车应用程序。 cart表的结构为userid | productid | quantity | postingdate userid | productid | quantity | postingdate userid | productid | quantity | postingdate 。 在基于会话的实现中,特定产品的数量信息存储在$_SESSION['cart'][$row['id']]['quantity']; ; 我在从数据库的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
}
}
}
我试过$row['id']但是它从$query返回的记录中返回所有productid的数组,而MySQl似乎不接受它,而我需要$getquantity在每个productid上运行。 请不要介意它很容易受到SQL注入的影响,因为我将在以后的阶段中对此进行介绍。