Home  >  Q&A  >  body text

add to cart using php

I want to add product to cart.. I am using session to add new product to cart but I don't know why when I am using "left join" in database I am not receiving any result even when I enter the command manually it is showing the table but here Nothing happened. That's the 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;  
}
}
?>

No products added! please help me

P粉878510551P粉878510551224 days ago357

reply all(1)I'll reply

  • P粉328911308

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

    It seems to me that you forgot the $ sign before the variable col in the query

    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 should be $col

    Better yet, you can write:

    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

    reply
    0
  • Cancelreply