1
我正在尝试将项目存储到会话(访客用户)并在购物车中显示项目。我可以看到会话日志文件存在:未存储到会话的项目
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 & 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>";
}
无处存在你曾经在$ _SESSION中存储过任何东西。你只有一个来自会话的READING实例(一个foreach循环)。 –
@MarcB我的道歉请看我更新的问题。 – PhpDude
基本调试,然后:你是否检查会话是否实际启动成功?你只是说“在标题”,但没有多大意义。 '
<?php session_start(); ?>“在标题中是”,但仍然是“标题已发送”警告。 –