Question

I have a simple php checkout but when adding the product to basket i get the following error:

PHP Notice: Undefined offset: 36 in C:\inetpub\wwwroot\shopping\cart.php on line 76

If i then refresh the page or go back to add another product it displays a product fine, it seems to be when adding to first product to the cart we get this error. any help would be much appreciated as all the answers i have tried have not worked.

 <?php
                        // connect to the database
                        include("databasedrop.php");
 ?>


                        <div class="post">
                            <h2>Shopping Basket<span class="title-bottom">&nbsp;</span></h2>

                        </div>
                        <!-- End Post -->

                <?php
                if (empty($_GET['id'])) {
                    $_GET['id'] = "";
                }


                $ProductID = $_GET['id'];  //the product id from the URL 
                $action = $_GET['action']; //the action from the URL 
                //if there is an product_id and that product_id doesn't exist display an error message


                switch ($action) { //decide what to do  
                    case "add":
                        $_SESSION['cart'][$ProductID] ++; //add one to the quantity of the product with id $product_id 
                        break;

                    case "remove":
                        $_SESSION['cart'][$ProductID] --; //remove one from the quantity of the product with id $product_id 
                        if ($_SESSION['cart'][$ProductID] == 0)
                            unset($_SESSION['cart'][$ProductID]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. 
                        break;

                    case "empty":
                        unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
                        break;
                }
                ?>              


                <?php
                if (empty($_SESSION['cart'])) {
                    $_SESSION['cart'] = "";
                }
                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 $ProductID => $quantity) {

                        //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
                        if ($stmt = $conn->prepare("SELECT * FROM products WHERE ProductID=?")) {
                            $stmt->bind_param("i", $ProductID);
                            $stmt->execute();

                            $stmt->bind_result($ProductID, $Title, $Description, $Price, $Stock, $Image, $Category, $Status);
                            $stmt->fetch();

                            // show the form
                        }



                        $total = "";
                        $line_cost = $Price * $quantity;  //work out the line cost
                        $total = $total + $line_cost;   //add to the total cost

                        echo "<tr>";
                        //show this information in table cells
                        echo "<td align=\"center\">$Title</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\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$ProductID\">X</a></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=empty\" onclick=\"return confirm('Are you sure?');\">Empty 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.";
                }


                ?>
Was it helpful?

Solution

Shot in the dark, but only solution I can see.

Do you ever create the $_SESSION['cart']-array?

Try this:

switch ($action) {
    case "add":
    if (!isset($_SESSION['cart'][$ProductID])) {
        $_SESSION['cart'][$ProductID] = 1;
    }
    else {
        $_SESSION['cart'][$ProductID]++;
    }
    break;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top