0

This question has been asked many times in the forum but none have been answered clearly. I am new to PHP programming and was trying to build a shopping cart using PHP sessions. When the user clicks the delete link in my cart, the quantity is reduced, but when the quantity is less than 1 my program shows errors below:

Notice: Undefined offset: 0 in C:\xampp\htdocs\mycart\functions.php on line 25

Notice: Trying to get property of non-object in C:\xampp\htdocs\mycart\functions.php on line 25

Notice: Use of undefined constant item_delete - assumed 'item_delete' in C:\xampp\htdocs\mycart\cart.php on line 47

Here is the source code of my cart.php file

<?php

require 'init.php';
require 'item.php';


//adding to cart

if (isset($_GET['pid']) && empty($_GET['pid']) === false) {

    $query = 'SELECT * FROM products where id=' . $_GET['pid'];
    $result = $con->query($query);
    $product = mysqli_fetch_object($result);

    $item = new item();

    $item->id = $product->id;
    $item->name = $product->name;
    $item->price = $product->price;
    $item->quantity = 1;

    $index = in_cart($item->id);
    $cart = unserialize(serialize($_SESSION['cart']));

    if ($index === false) {
        $_SESSION['cart'][] = $item;
    } else {
        $cart[$index]->quantity++;
        $_SESSION['cart'] = $cart;
    }

        header("Location:displaycart.php");

 }else if ( isset($_GET['view']) ){
    disp_cart();
    
    }


if(isset($_GET['delete']) && !empty($_GET['delete'])){

        $cart=unserialize(serialize($_SESSION['cart']));
        $item_delete=in_cart($_GET['delete']);

        if(empty($cart)){
            echo "Cart Already Empty <br></Emptybr><a href='index.php'>Continue Shopping</a>";
        }else if(!item_delete){
            echo "Item Not Found In Cart <br><a href='index.php'>Continue Shopping</a>";
        }else if($cart[$item_delete]->quantity==1){
            
            unset($cart[$item_delete]);
            header("Location:index.php");
            //echo"Item Deleted <br><a href='index.php'>Continue Shopping</a>";
        }else{
            $cart[$item_delete]->quantity--;
        }
$_SESSION['cart']=$cart;
    }


?>
Community
  • 1
  • 1
Ayusch
  • 389
  • 4
  • 17
  • Look at the lines mentioned, for example `}else if(!item_delete){`... Also you are open to SQL injections.. Where is `in_cart`, is that line 25? – chris85 Jul 11 '15 at 13:07
  • in_cart is a function I have created to check wether item is already in cart or not. It returns the index of item if it is present otherwise it returns false. I am just testing the cart so i didn't care for SQL injections right now. I will sanitize the data later. – Ayusch Jul 11 '15 at 13:15
  • My in_cart function : function in_cart($id) { $cart = unserialize(serialize($_SESSION['cart'])); for ($i = 0; $i < count($cart); $i++) { if ($cart[$i]->id === $id) { return $i; } } return false; } – Ayusch Jul 11 '15 at 13:18
  • The general, common reasons for _all three_ of those notices have been explained plenty often and in quite broad detail here already. Please refer to [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php), and make at least an effort to debug this yourself using the information you find there. – CBroe Jul 11 '15 at 13:49

0 Answers0