0

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>

Edited by devianleong

4
Contributors
14
Replies
17
Views
5 Years
Discussion Span
Last Post by devianleong
Featured Replies
  • 1

    Line 95 shows: `name=\"qty[$id]\"` so it's not `qty` in your POST array. I think you need to change that to:`name=\"qty[]\"` to be able to use the foreach. Read More

  • > <a href=\"$_SERVER[PHP_SELF]?action=update\" onclick=\"return confirm('Are you sure?');\">Update Cart</a> your not posting your form you need to use a button or js to submit your form @pritaeas name is fine it has the same effect Read More

  • 1

    Overlooked the fact that it was a link (and not a submit button as the title suggests). I stand corrected. Read More

  • 1

    Hi, take this for your consideration... on your form method it show post. I am assuming here that the above script is called cart.php as shown in your form codes below. <form action="cart.php" method="post"> <input type="hidden" name="action" value="update" /> while you are processing it as $_GET as shown on your … Read More

1

Line 95 shows: name=\"qty[$id]\" so it's not qty in your POST array. I think you need to change that to:name=\"qty[]\" to be able to use the foreach.

Edited by pritaeas

1

<a href=\"$_SERVER[PHP_SELF]?action=update\" onclick=\"return confirm('Are you sure?');\">Update Cart</a>

your not posting your form
you need to use a button or js to submit your form

@pritaeas name is fine it has the same effect

Votes + Comments
Spot on
0

name is fine it has the same effect

Hmz... if it was fine, he would not see: Notice: Undefined index: qty

1

Overlooked the fact that it was a link (and not a submit button as the title suggests). I stand corrected.

Edited by pritaeas

Votes + Comments
Hi,I had change that to name=\"qty[]\"I even tried name=\"qty\" but still not working..it's show the same error message
0

Hi,I had change that to name=\"qty[]\"I even tried name=\"qty\" but still not working..it's show the same error message

0

or anyone can help me change the code? I just want the update function work =(

1

Hi,

take this for your consideration... on your form method it show post. I am assuming here that the above script is called cart.php as shown in your form codes below.

<form action="cart.php" method="post">

<input type="hidden" name="action" value="update" />

while you are processing it as $_GET as shown on your codes in line 43

elseif(isset($_GET['action']) && ($_GET['action'] =='update')){

If possible, change the above to this

elseif(isset($_POST['action']) && ($_POST['action'] =='update')){

## then do the things they suggested here.

I hope I contributed something here, if not, I wish you good luck .

Edited by veedeoo: more info added

Votes + Comments
Hi veedeoo , I had change to $_post and the error gone. But now the another problem is when I enter 2 in the quantity field and click update, the quantity still remain the same. Any solution ?
0

Hi veedeoo , I had change to $_post and the error gone. But now the problem is when I enter 2 in the quantity field and click update, the quantity still remain the same. Any solution ?

0

all you need to do is to add the posted value to the old value which is in your cart session kind of like this. There are other ways of doing this, I am just showing you an example.

## this is your session before the submission
$before = $_SESSION['cart'][$pid];

## this is your newly posted quantity on top of what is already in the session

$addedNewQuantity = $v;
$newQuantity = "";

## add the added new quantity to your before
## you need to create a new condition if the customer is trying to add or remove
if($add){
$newQuantity .= $before + $addedNewQuantity;
}

if($remove){
## removing quantity , make sure the cart is not empty, so that is another environment you need to set.
$newQuantity .= $before - $addedNewQuantity;

}
## unset the session to reflect updated quantity
unset($_SESSION['cart'][$pid]);

## set the new session reflecting the new updated quantity
unset($_SESSION['cart'][$pid]) = (int)$newQuantity;
0

Anyone can help??The error gone now since veedeoo solve the error

Hi,

take this for your consideration... on your form method it show post. I am assuming here that the above script is called cart.php as shown in your form codes below.

<form action="cart.php" method="post">
<input type="hidden" name="action" value="update" />

while you are processing it as $_GET as shown on your codes in line 43

>

elseif(isset($_GET['action']) && ($_GET['action'] =='update')){

If possible, change the above to this

>

elseif(isset($_POST['action']) && ($_POST['action'] =='update')){

then do the things they suggested here.

I hope I contributed something here, if not, I wish you good luck .

But now the another problem appear which is when I set a new quantity and click "Update", the quantity still remain the same . Any solution for this problem?

0

Thank all..Finally I solve the problem.I had change the code to :

elseif(isset($_POST['action']) && ($_POST['action'] =='update')){
    //

    foreach ($_POST['qty'] as $id => $v){
            $qty = (int)$v; 
             if ($qty > 0){
                $_SESSION['cart'][$id] =$qty; 


             }
             if($qty == 0){
                 unset($_SESSION['cart'][$id]);
             }
        }


    echo'Your cart has been updated';
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.