The shopping cart shows all the product, but when I change the quantity and click update, the error will pop out and display the following error and the quantity still remain and no any changes:
( ! ) Notice: Undefined index: qty in C:\wamp\www\cart.php on line 46
Call Stack
# Time Memory Function Location
1 0.0007 703088 {main}( ) ..\cart.php:0
( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\www\cart.php on line 46
Call Stack
# Time Memory Function Location
1 0.0007 703088 {main}( ) ..\cart.php:0
**Line 46 ** :
foreach ($_POST['qty'] as $k => $v){
**Below is the entire source code: **
<?php
include('./include/config.php');
include('./include/loginverify.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="cart.php" method="post">
<input type="hidden" name="action" value="update" />
<?php
//determind add action
if(isset($_GET['action']) && ($_GET['action'] == 'add')){
//determind ID from url
if(isset($_GET['id'])){
//determind the value is positive or not
if($_GET['id']> 0){
//declare ID as $_GET['ID'] from url
$id =(int) $_GET['id'] ;
$query = "SELECT * FROM product WHERE ID = $id";
$result = mysql_query($query,$con);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]++;
echo'Another copy has been added to cart';
}
else{
$_SESSION['cart'][$id] = 1;
echo'This item has been added to your cart';
}
}
}
}
//if the action is update in url
elseif(isset($_GET['action']) && ($_GET['action'] =='update')){
//
foreach ($_POST['qty'] as $k => $v){
$pid=(int) $k;
$qty=(int) $v;
if ($qty ==0){
unset($_SESSION['cart'][$pid]);
}elseif($qty>0){
$_SESSION['cart'][$pid] = $qty;
}
}
echo'Your cart has been updated';
}
?>
<?php
if($_SESSION['cart']) { //if the cart isn't empty
//show the cart
echo "<table border=\"1\" padding=\"3\" width=\"40%\">"; //format the cart using a HTML table
//iterate through the cart, the $product_id is the key and $quantity is the value
foreach($_SESSION['cart'] as $id => $v) {
//get the name, description and price from the database - this will depend on your database implementation.
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT Name, Rent_price, Category FROM product WHERE id = %d;",
$id);
$result = mysql_query($sql);
//Only display the row if there is a product (though there should always be as we have already checked)
if(mysql_num_rows($result) > 0) {
list($Name, $Rent_price, $Category) = mysql_fetch_row($result);
$total = 0;
$line_cost = $_SESSION['cart'][$id] * $Rent_price; //work out the line cost
$total +=$line_cost;
//add to the total cost
echo "<tr>";
//show this information in table cells
echo "<td align=\"center\">$Name</td>";
//along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
echo "<td align=\"center\"><input type = \"text\" size=\"3\" name=\"qty[$id]\" value =\"{$_SESSION['cart'][$id]}\"> </td>";
echo "<td align=\"center\">$line_cost</td>";
echo "</tr>";
}
}
//show the total
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\">Total</td>";
echo "<td align=\"right\">$total</td>";
echo "</tr>";
//show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
echo "<tr>";
echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=update\" onclick=\"return confirm('Are you sure?');\">Update Cart</a></td>";
echo "</tr>";
echo "</table>";
}else{
//otherwise tell the user they have no items in their cart
echo "You have no items in your shopping cart.";
}
function productExists($id) {
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT * FROM product WHERE id = %d;", $id);
return mysql_num_rows(mysql_query($sql)) > 0;
}
?>
</form>
</body>
</html>