Heim  >  Fragen und Antworten  >  Hauptteil

Mit PHP zum Warenkorb hinzufügen

Ich möchte das Produkt in den Warenkorb legen. Ich verwende die Sitzung, um ein neues Produkt in den Warenkorb zu legen, aber ich weiß nicht, warum ich bei Verwendung von „Left Join“ in der Datenbank kein Ergebnis erhalte, selbst wenn ich den Befehl manuell eingebe. Es wird die Tabelle angezeigt, aber hier ist nichts passiert. Das ist das Problem!

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

Keine Produkte hinzugefügt! Bitte hilf mir

P粉878510551P粉878510551174 Tage vor268

Antworte allen(1)Ich werde antworten

  • 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

    Antwort
    0
  • StornierenAntwort