Reputation: 17
I'm working on my homework. I have a cart session and I can get on the attribute in mySql database base on the product id.
<?php
$total_price=0;
if(isset($_SESSION['cart']) ? $_SESSION['cart'] : null)
{
echo "<tr>
<th></th>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
</tr>";
foreach ($_SESSION['cart'] as $key => $product) {
$the_query = "SELECT * FROM products WHERE id=" . $product->id;
$the_product = $db->query($the_query) or die('Query failed: '.mysql_error());
$the_product->execute();
$the_product->setFetchMode(PDO::FETCH_OBJ);
while ($row = $the_product->fetch()){
$total_price = $total_price + $row->price*$product->quantity;
echo "<tr><td>";
echo "<img src='".$row->image_url_small."' /></a></td>";
echo "<td><strong>".$row->name."</strong></td><td><em>$".$row->price."</em>";
echo '</td>';
echo '<td><input type="text" id="'.$row->id.'" class="override" value="'.$product->quantity.'"/></td>';
echo '<td><a href="do_deletecart.php?id="'.$row->id.'">Delete item </a></td></tr>';
}}
echo "<tr><td colspan='2'></td></tr>";
echo "<tr><td style='text-align:center;font-size:40px;'>$</td><td><strong>Total</strong><br /><em>$".$total_price."</em></td></tr>";
}
else {
echo "Your cart is empty.";
}
?>
Update I can pass the id to do_deletecart.php. But now I can delete the product from cart do_deletecart.php
<?php
session_start();
$product = $_GET['id'];
foreach($_SESSION['cart'] as $key=>$value) {
if($product == $value)
{
unset($_SESSION['cart'][$key]);
break;
} }
header("location:cart.php");
?>
Upvotes: 0
Views: 161
Reputation: 53208
Well, assuming that $row->id
contains what you expect, you have enclosed it with quote marks, which will essentially terminate the <a>
element's href
attribute.
You need to update your code as follows:
echo '<td><a href="do_deletecart.php?id='.$row->id.'">Delete item </a></td></tr>';
Also, you might want to check that you have actually started the session. In order to access the $_SESSION
superglobal, you need to first have called session_start()
before any output was sent to the browser.
Upvotes: 1
Reputation: 1845
You need to make sure you include
session_start();
before using $_SESSION
Upvotes: 0