-2

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";
}
}
}


?>
WibblyWobbly
  • 145
  • 3
  • 7
  • 18
  • What precisely is your error? We need to know how you store the items in addition to how it is displayed – jh314 Jun 14 '13 at 22:08
  • Your code only shows the reading section of your script. You are asking why a product isn't added to your cart (the "writing" section), so your code is useless for this question. – Alejandro Iván Jun 14 '13 at 22:09
  • Thats the thing, there is no error output. – WibblyWobbly Jun 14 '13 at 22:10
  • What he means is, this is the section that "fetches" stuff to display. You want the section that puts it in the cart, i.e. something along the lines of "Insert stuff into cart" instead of "Select stuff from cart" – JohnP Jun 14 '13 at 22:16

2 Answers2

0

You have brackets in your query.

Change

$sql2 = "SELECT * FROM products WHERE SKU=[$id]";

to

$sql2 = "SELECT * FROM products WHERE SKU=$id";
immulatin
  • 2,118
  • 1
  • 12
  • 13
0

My guess is that if(isset($_SESSION['cart'])) is returning false .. based on your code, nothing would be output if this is correct.

edit:

I see actually echo "<p>Your part is empty. <br />Please add some products</p>"; would be executed, but still - I don't see $_SESSION['cart'] being initialised/set anywhere..

Alfie
  • 2,341
  • 2
  • 28
  • 45
  • Shouldn't the section `

    ` be the initialising?
    – WibblyWobbly Jun 14 '13 at 22:43
  • PHP is odd, without testing I'm not sure - but I would not be surprised if it was considered a totally separate variable by the PHP interpreter.. I would try setting $_SESSION['cart'] directly. I would also suggest that you use classes to encapsulate your cart behaviour - you can then do something like `$_SESSION['cart'] = new clsCart();`, which would contain `item/product` entries. You would then check how many items/products were in the cart to determine the 'emptiness' of the cart. Hope this makes sense. – Alfie Jun 14 '13 at 22:52
  • Also - remember to add `session_start();` to the beginning of any PHP files requested via AJAX – Alfie Jun 14 '13 at 22:55
  • I have changed a bit in my code the `Add to cart` bit was wrong, i changed products&actions to products&action, however I now get the product invalid message immediately – WibblyWobbly Jun 15 '13 at 14:33
  • Well I suppose if you are getting the 'This product ID is invalid' error, your database query must be returning 0 results as per the line `if(mysql_num_rows($query2) != 0){` .. Also, I notice a typo in the following line: `mysql_fetch_array($qery2)` should be `mysql_fetch_array($query2)` (note the **U**), although this won't be the cause of your issue. Make sure the `id` parameter you are passing in the link relates to an existing row in your table. Also, your code is vulnerable to SQL injection: http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php?rq=1 – Alfie Jun 15 '13 at 22:50