首頁  >  問答  >  主體

使用 php 加入購物車

我想將產品加入購物車.. 我使用會話將新產品添加到購物車但是我不知道為什麼當我在資料庫中使用“左連接”時,我沒有收到任何結果,即使當我手動輸入命令時,它也會顯示表格,但是這裡什麼也沒發生。 問題就在這裡!

<?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;  
}
}
?>

沒有增加任何產品! 請幫我

P粉878510551P粉878510551174 天前266

全部回覆(1)我來回復

  • P粉328911308

    P粉3289113082024-04-02 11:10:07

    在我看來,您忘記了查詢中變數 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

    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

    回覆
    0
  • 取消回覆