Maison  >  Questions et réponses  >  le corps du texte

ajouter au panier en utilisant php

Je souhaite ajouter un produit au panier. J'utilise une session pour ajouter un nouveau produit au panier mais je ne sais pas pourquoi lorsque j'utilise "jointure gauche" dans la base de données, je ne reçois aucun résultat même lorsque j'entre la commande manuellement, le tableau affiche mais ici rien ne s'est passé. C'est le problème!

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

Aucun produit ajouté ! S'il vous plaît, aidez-moi

P粉878510551P粉878510551174 Il y a quelques jours269

répondre à tous(1)je répondrai

  • P粉328911308

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

    Il me semble que vous avez oublié le signe $ avant la variable col dans la requête

    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 devrait être $col

    Mieux encore, vous pouvez écrire :

    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

    répondre
    0
  • Annulerrépondre