我想用 PHP 添加产品到购物车

问题描述 投票:0回答:0

当我点击将产品添加到购物车时,它会显示:“1 = 0”并且没有产品名称或价格等...有人可以帮忙吗? (我应该提到 init.inc 文件具有有效的数据库连接)

这是代码:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Start the session
require_once 'inc/init.inc.php';


// check if the cart array exists in the session, if not create it
if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = array();
}

// check if the product has been added to cart
if (isset($_POST['add_to_cart'])) {
    // get product details
    $product_id = $_POST['product_id'];
    $product_name = $_POST['product_name'];
    $price = $_POST['price'];
    $quantity = $_POST['quantity'];

    // create an array of product data
    $product_data = array(
        'product_id' => $product_id,
        'product_name' => $product_name,
        'price' => $price,
        'quantity' => $quantity
    );

    // add product data to cart array
    $_SESSION['cart'][$product_id] = $product_data;
}



?>

还有这个:

        <div class="text-dark">
            <?php
            // display cart items
            if (empty($_SESSION['cart'])) {
                echo "Your cart is empty";
            } else {
                foreach ($_SESSION['cart'] as $product) {
                    echo "<p class=\"text-dark\">" . $product['product_name'] . "  " . $product['quantity'] . " = " . floatval($product['price']) * intval($product['quantity']) . "</p>";

                }
            }

            ?>
        </div>

我在这里开始会议:

<?php

// 1. connecting to DB

$pdoBlog = new PDO(
        'mysql:host=localhost;
        dbname=onye',
        'root',
        'root',
        array(
            PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',

        )
    );
    // 2. starting a session
    session_start();

    //3 . a variableto show the messages(error or success)
    $contenu = '';
    /* this variable will be exclusively used to show the errors and the success messages, we leave it empty and concatenate it with:  .=  to show the messages*/
    

    //4. including the functions.php file
    
    require_once 'functions.inc.php';

这是行不通的:

foreach ($_SESSION['cart'] as $product) {
                        echo "<p class=\"text-dark\">" . $product['product_name'] . "  " . $product['quantity'] . " = " . number_format(floatval($product['price']), 2) * intval($product['quantity']) . "</p>";

因为我希望它显示产品名称等...不仅 1=0 1 是数量。我四处移动并尝试以不同的方式调用变量。

这是添加到购物车的产品的 $_POST 表单:

    <form method="POST" action="cart.php">
        <input type="hidden" name="product_id" value="<?php echo $_GET["product_id"]; ?>">
        <input type="hidden" name="product_name" value="<?php echo $_GET["product_name"]; ?>">
        <input type="hidden" name="price" value="<?php echo $_GET["price"]; ?>">
        <label>Quantity: </label>
        <input type="number" name="quantity" value="1" min="1">
        <button type="submit" name="add_to_cart">Add to Cart
            <?php
            // Get the number of items in the cart
            $cart_count = isset($_SESSION['cart']) ? count($_SESSION['cart']) : 0;

            // Display the cart count
            echo "($cart_count)";
            ?>
        </button>
    </form>
php shopping-cart
© www.soinside.com 2019 - 2024. All rights reserved.