我想将产品添加到购物车.. 我使用会话将新产品添加到购物车但是我不知道为什么当我在数据库中使用“左连接”时,我没有收到任何结果,即使当我手动输入命令时,它也会显示表格,但是这里什么也没发生。 问题就在这里!
<?php
$conn=mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn){
die('Could not Connect My Sql:' .mysql_error());
}
if(!empty($_GET["action"])) {
switch(inj($_GET["action"])) {
case "add":
if(!empty(inj($_POST["quantity"]))) {
$cod=$_GET['code'];
$col=$_POST['colo'];
$productByCode = $db_handle->runQuery("SELECT tblproduct.*, colors.color FROM tblproduct LEFT JOIN colors ON tblproduct.id=colors.cl_id
WHERE tblproduct.code='".$cod."' AND colors.color='".col."' ORDER BY tblproduct.code");
$itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>inj($_POST["quantity"]), 'price'=>inj($_POST["ggg"]),'color'=>$col));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["code"],array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["code"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += inj($_POST["quantity"]);
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if(inj($_GET["code"]) == $k)
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}
?>
没有添加任何产品! 请帮助我
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
在我看来,您忘记了查询中变量 col 之前的 $ 符号
col 应该是 $col
更好的是,你可以这样写:
SELECT tblproduct.*, colors.color FROM tblproduct LEFT JOIN colors ON tblproduct.id=colors.cl_id WHERE tblproduct.code='{$cod}' AND colors.color='{$col}' ORDER BY tblproduct.code