没有存储到会话的项目

时间:2016-07-27 14:24:04

标签: php session

我正在尝试将项目存储到会话(访客用户)并在购物车中显示这些项目。我可以看到会话存在于日志文件中:

products|a:1:{i:0;a:6:{s:3:"img";s:62:"http://localhost/eCommerce%20Project/uploads/womens/w_nike.jpg";s:2:"id";s:1:"3";s:4:"name";s:11:"womens nike";s:5:"price";s:2:"14";s:3:"sku";s:3:"456";s:11:"description";s:20:"Womens Nike trainers";}}

这是我的代码,它允许我将项目发布到会话,这是一个产品的单一视图:

$product_id = $_GET['product_id'];
$sql = "SELECT * FROM Products WHERE product_id = $product_id";
    $result = $conn->query($sql);

    if($result->num_rows > 0){
        while($row = $result->fetch_assoc()){
            echo '<img src=' . $row['product_img'] . ' />';
            echo '<div class="title">'. $row['product_name'] .'</div>';
            echo '<div class="title">'. $row['product_price'] .'</div>';
            echo '<div class="title">'. $row['product_sku'] .'</div>';
            echo '<div class="title">'. $row['product_description'] .'</div>';
            echo '<div class="title">'. $row['gender'] .'</div>';
            echo '<form action="addToCart.php" method="post">
                    <input type="hidden" name="product_img" value="' . $row['product_img'] . '" />
                    <input type="hidden" name="product_id" value="' . $row['product_id'] . '" />
                    <input type="hidden" name="product_name" value="' . $row['product_name'] . '" />
                    <input type="hidden" name="product_price" value="' . $row['product_price'] . '" />
                    <input type="hidden" name="product_sku" value="' . $row['product_sku'] . '" />
                    <input type="hidden" name="product_description" value="' . $row['product_description'] . '" />
                    <button name="addToBasket">Add to basket</button>
                  </form>';

            $gender = $row['gender'];  
        }

    }

会话在标题中设置,因此无论您在哪个页面,始终都会开始。

我的购物车如下所示,这里有很多内容,但基本上这个项目并没有显示在购物篮中。

$sum = 0;

foreach ($_SESSION['products'] as $product) {
    $name = $product['name'];
    $id = $product['id'];
    $price = $product['price'];
    $img = $product['img'];
    $sku = $product['sku'];
    $description = $product['description'];

    echo '<a href="single_product.php?product_id=' . $product['id'] . '">';
    echo "<img src='$img'><br />";
    echo "Product: $name<br />";
    echo "Price: $price | ID: $id<br />";
    echo "$description";
    echo '</a><br /><br />';
    echo '<form action="removeItem.php" method="post">
            <input type="hidden" name="product_id" value="' . $product['id'] . '" />
            <button name="removeItem">Remove</button>
          </form>';    

    $sum += $price;

}

if($product == 0){
    echo 'Sorry no items in basket <a href="/eCommerce%20Project/">Continue shopping</a>';
}

    echo "<h4>Sub total £" . $sum . "</h4>";

    // Calulating delivery costs
    $deliveryCharge = 0; // Initial value
    if($sum > 50 || $product == 0){ // If more than £50 deliver is 0
        $deliveryCharge = 0;
    } else if($sum < 50){ // If less than £50 delivery is £15
         $deliveryCharge = 15;
    };

    // Output of delivery cost
    echo "<h4>Delivery £" . $deliveryCharge . "</h4>";    

    // Calculation for working out 20% on top of price
    $percentage = 20;
    $totalPrice = $sum;
    $totalPrice = ($percentage / 100) * $totalPrice;

    // Calculation to find totals for price + vat + delivery
    $totalPriceWithVat = $sum + $totalPrice + $deliveryCharge;

    // Output full total
    echo "<h4>Total (Inc. VAT &amp; Delivery) £" . $totalPriceWithVat . "</h4>";

    echo '<button>Checkout</button>';

以下是执行帖子的表单:

if (isset($_POST['addToBasket'])) { 
    $_SESSION['products'][] = array(
        'img' => $_POST['product_img'],
        'id' => $_POST['product_id'],
        'name' => $_POST['product_name'],
        'price' => $_POST['product_price'],
        'sku' => $_POST['product_sku'],
        'description' => $_POST['product_description']
    );
echo "<script type='text/javascript'>  window.location='basket.php'; </script>";
} 

1 个答案:

答案 0 :(得分:1)

确保会话处于活动状态,否则请使用 session_start() 之前 if (isset($_POST['addToBasket'])) {