簡體   English   中英

從會話購物車中讀取多個項目時出現問題

[英]Problem when reading multiple items from session shopping cart

我對 PHP 有點陌生,我從頭開始構建了一個頁面。 我在網上找到了一個購物車代碼,我拿來使用,效果很好:

if(isset($_POST["add_to_cart"]))
{
    if(isset($_SESSION["shopping_cart"]))
    {
        $item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
        if(!in_array($_GET["id"], $item_array_id))
        {
            $count = count($_SESSION["shopping_cart"]);
            $item_array = array(
                'item_id'           =>  $_GET["id"],
                'item_name'         =>  $_POST["hidden_name"],
                'item_price'        =>  $_POST["hidden_price"],
                'item_quantity'     =>  $_POST["quantity"]
            );
            $_SESSION["shopping_cart"][$count] = $item_array;
        }
        else
        {        }
        
    }
    else
    {
        $item_array = array(
            'item_id'           =>  $_GET["id"],
            'item_name'         =>  $_POST["hidden_name"],
            'item_price'        =>  $_POST["hidden_price"],
            'item_quantity'     =>  $_POST["quantity"]
        );
        $_SESSION["shopping_cart"][0] = $item_array;
    }
}

if(isset($_GET["action"]))
{
    if($_GET["action"] == "delete")
    {
        foreach($_SESSION["shopping_cart"] as $keys => $values)
        {
            if($values["item_id"] == $_GET["id"])
            {
                unset($_SESSION["shopping_cart"][$keys]);
                echo '<script>window.location="grid.php"</script>';
            }
        }
    }
} 

這讓我可以將多個產品、數量和價格添加到購物車。 我現在要做的是從該采購明細創建采購訂單,假設有人購買了 2 個產品,我需要插入 SQL 表 2 行,每個產品一個。

經過一些谷歌,我想我必須使用 foreach ,我最終得到了這個:

$_SESSION['shopping_cart'][] = $result;
foreach ($_SESSION['shopping_cart'] as $result){
    echo "Item id:  ".$result['item_id']." , <br>\n 
          cantidad: ".$result['item_quantity']." ,<br>\n 
          Precio unitario: ".$result['item_price']."<br>\n";
}

這將返回頁面上的下一個錯誤:


Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\PHP\Proyecto\Crear_OC.php on line 79

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\PHP\Proyecto\Crear_OC.php on line 80

我想要做的是回顯所有產品的數量、總價和單價,看看我是否做對了,如果我能做到,那么我有這個代碼來執行插入到 SQL 中:

$total = $total + ($values["item_quantity"] * $values["item_price"]);
    $sql="  INSERT INTO ordenes_ventas (ID_Producto,ID_Cliente,Precio_Unitario,Cantidad,Monto)
                    select p.ID_Producto,C.ID_Cliente,p.Precio_Unidad, $result['item_quantity'],$total 
                    from productos p 
                    join clientes c 
                    on c.ID_Cliente = (select c.ID_Cliente from clientes c where c.Users = '$user') 
                    where p.ID_Producto = $result['item_id'] ";

    if ($conexion->query($sql) === TRUE) {
    echo 'Succesful!';
    } else {
    echo "Error: " . $sql . "<br>" . $conexion->error;
}
}

您想要做的是使用購物車中的會話號並執行 MySQL 語句來查找客戶一直在購買的商品。 這確保如果兩個客戶同時使用購物車,他們的訂單不會混淆並同時加載。

我包含了我在一個項目中完成的一些代碼,您可以查看這些代碼並將其合並到您的代碼中。

您需要捕獲個人的會話編號:

/*insert the content into the cart table*/
        $addCart = 'INSERT INTO cart (cartId, productId, quantity_order, sessionNumber) VALUES ("","'.$productId.'","'.$quantity.'","'.$sessionNumber.'")';

將商品放入購物車后,您現在可以使用另一個 SQL 語句查找客戶購物車中的當前商品。

$findCart = "SELECT p.productId, p.name, p.image, p.price, p.quantity, c.cartId, c.quantity_order, c.sessionNumber FROM product p, cart c WHERE p.productId = c.productId AND c.sessionNumber = '$sessionNumber' "; 

我認為這種方法會比數組語句流暢得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM