I'm currently designing a shopping cart following a tutorial online.
I have followed as much as I can down to each detail, with some changes such as variable names etc changing.
I have checked through the code below, however when I clicked "Add To Cart" it doesn't display the added item in the side bar.
Any suggestions as to what the issue may be? (The code is only the segment relating to the sidebar, the products php is in a separate php file.)
<div id="sidebar">
<h1>Cart</h1>
<?php
if(isset($_SESSION['cart'])){
$sql = "SELECT * FROM products WHERE SKU IN(";
foreach($_SESSION['cart'] as $id => $value){
$sql .= $id. ",";
}
$sql = substr($sql,0,-1) . ") ORDER BY SKU ASC";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
?>
<p><?php echo $row['name']; ?><?php echo $_SESSION['cart'][$row['SKU']]['quantity']; ?></p>
<a href="index.php?page=cart">Go To Cart</a>
<?php
}
}else {
echo "<p>Your part is empty. <br />Please add some products</p>";
}
?>
</div>
Section of code from products PHP file which does the adding:
<?php
if(isset($_GET['action']) && $_GET['action'] == "add"){
$id = $_GET['id'];
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
} else {
$sql2 = "SELECT * FROM products WHERE SKU=$id";
$query2 = mysql_query($sql2);
if(mysql_num_rows($query2) != 0){
$row2 = mysql_fetch_array($qery2);
$_SESSION['cart'][$row2['SKU']] = array("quantity" => 1, "price" => $row2['price']);
} else {
$message = "This product ID is invalid";
}
}
}
?>