The problem: Nothing I have tried over the last 2 days has gotten anywhere near fixing this problem.
I’ve been learning about shopping carts from the Welling & Thomson PHP and MySQL Web Dev. book (fourth ed.)
The project I am learning from is a shopping cart for books. Ordinarily, the supplied code would work IF all books have an ISBN. However books prior to some time in the seventies don’t have an ISBN.
So rather than devise a book code with some trepidation I used a book’s ‘title’ as the DB primary key.
Everything works except the removal of a book from the shopping cart i.e. setting the quantity on the form to ‘0’.
This is snippets of the setup for the show_cart form.
<form action=\"show_cart.php\" method=\"post\">
...
...
echo "<td align=\"left\">
<a href=\"show_book.php?title=".$title."\">".$book['title']."</a>
by ".$book['author']."</td>
<td align=\"center\">£".number_format($book['price'], 2)."</td>
<td align=\"center\">";
// if we allow changes, quantities are in text boxes
if ($change == true) {
echo "<input type=\"text\" name=\"".$title."\" value=\"".$qty."\" size=\"3\">";
} else {
echo $qty;
}
echo "</td><td align=\"center\">£".number_format($book['price']*$qty,2)."</td></tr>\n";
}
…
…
if($change == true) {
echo "
<td colspan="".(2+$images)."">
<td align=“center”>
<input type=“hidden” name=“save” value=“true”/>
<input type=“image” src=“images/save-changes.gif”
border=“0” alt=“Save Changes”/>
}
I then run the scripts and choose a couple of books for my cart THEN make one of the quantity fields zero and execute the following in show_cart.php which is more or less identical to the script in the book however my key is $title as opposed to $isbn.
if(isset($_POST[‘save’])) {
foreach ($_SESSION[‘cart’] as $title => $qty) {
if ($_POST[$title] == ‘0’) {
unset($_SESSION[‘cart’][’$title’]);
}
else {
$_SESSION[‘cart’][$title] = $_POST[$title];
}
}
$_SESSION['total_price'] = calculate_price($_SESSION['cart']);
$_SESSION['items'] = calculate_items($_SESSION['cart']);
}
The RESULT is all books disappear from the cart
Any suggestions would be gratefully received.
Jamie