Shopping Cart (unidentified index error)

I have two errors which exist on cart.php and I’m not sure how to solve them. The first is " Notice: Undefined index: item_quantity in C:\wamp\www\SC\cart.php on line 49" ($arrQuantity = $_POST[‘item_quantity’]:wink: and the second is “Notice: Undefined variable: s in C:\wamp\www\SC\cart.php on line 117” ( ).

The cart.php page should display the item details(ID, Name, Price, Quantity) from the database for the selected item in the index.php file when “Order Now” has been clicked. The index.php displays all the items from the database but when I click “Order Now” on any product, it goes into the the cart.php page but I get these errors which I cannot seem to solve. All help will be appreciated.

[php]// Start the session
session_start();
require ‘connect.php’;
require ‘item.php’;

if(isset($_GET[‘item_id’]) && !isset($_POST[‘update’])) {
$sql = “SELECT * FROM tbl_item WHERE item_id=”.$_GET[‘item_id’];
$result = mysqli_query($con, $sql);
$product = mysqli_fetch_object($result);
$item = new Item();
$item->item_id = $product->item_id;
$item->item_name = $product->item_name;
$item->item_price = $product->item_price;
$iteminstock = $product->item_quantity;
$item->item_quantity = 1;

// Check product is existing in cart
$index = -1;
$cart = unserialize(serialize($_SESSION['cart'])); // set $cart as an array, unserialize() converts a string into array
for($i=0; $i<count($cart);$i++)
    if ($cart[$i]->item_id == $_GET['item_id']){
        $index = $i;
        break;
    }
    if($index == -1) 
        $_SESSION['cart'][] = $item; // $_SESSION['cart']: set $cart as session variable
    else {

        if (($cart[$index]->item_quantity) < $iteminstock)
             $cart[$index]->item_quantity ++;
             $_SESSION['cart'] = $cart;
    }

}
// Delete product in cart
if(isset($_GET[‘index’]) && !isset($_POST[‘update’])) {
$cart = unserialize(serialize($_SESSION[‘cart’]));
unset($cart[$_GET[‘index’]]);
$cart = array_values($cart);
$_SESSION[‘cart’] = $cart;
}
// Update quantity in cart
if(isset($_POST[‘update’])) {
$arrQuantity = $_POST[‘item_quantity’];
$cart = unserialize(serialize($_SESSION[‘cart’]));
for($i=0; $i<count($cart);$i++) {
$cart[$i]->item_quantity = $arrQuantity[$i];
}
$_SESSION[‘cart’] = $cart;
}
?>

Items in your cart:

<th>Sub Total (CAD)</th>
<?php

if(isset($_GET[‘item_id’]) && !isset($_POST[‘update’])) {
$sql = “SELECT * FROM tbl_item WHERE item_id=”.$_GET[‘item_id’];
$result = mysqli_query($con, $sql);
$product = mysqli_fetch_object($result);
$item = new Item();
$item->item_id = $product->item_id;
$item->item_name = $product->item_name;
$item->item_price = $product->item_price;
$iteminstock = $product->item_quantity;
$item->item_quantity = 1;

// Check product is existing in cart
$index = -1;
$cart = unserialize(serialize($_SESSION['cart'])); // set $cart as an array, unserialize() converts a string into array
for($i=0; $i<count($cart);$i++)
    if ($cart[$i]->item_id == $_GET['item_id']){
        $index = $i;
        break;
    }
    if($index == -1) 
        $_SESSION['cart'][] = $item; // $_SESSION['cart']: set $cart as session variable
    else {

        if (($cart[$index]->item_quantity) < $iteminstock)
             $cart[$index]->item_quantity ++;
             $_SESSION['cart'] = $cart;
    }

?>

<?php $index++; } ?>
<tr>
    <td colspan="5" style="text-align:right; font-weight:bold">Sum 
     <input id="saveimg" type="image" src="images/save.png" name="update" alt="Save Button">
     <input type="hidden" name="update">
    </td>
    <td> <?php echo $s; ?> </td></tr>
Option Id Name Price Quantity
Delete <?php echo $cart[$i]->item_id; ?> <?php echo $cart[$i]->item_name; ?> <?php echo $cart[$i]->item_price; ?> <?php echo $cart[$i]->item_price * $cart[$i]->item_quantity; ?>

Continue Shopping | Checkout <?php if(isset($_GET["item_id"]) || isset($_GET["index"])){ header('Location: cart.php'); } ?> [/php]

Here’s index.php

[php]

<?php include 'connect.php'; require 'connect.php'; //select the test db mysqli_select_db($con, "test"); $sql = 'SELECT * FROM tbl_item'; $result = mysqli_query($con, $sql); ?>

Select the items:

<?php while($product = mysqli_fetch_object($result)) { ?> <?php } ?>
Id Name Price Quantity (in stock) Buy
<?php echo $product->item_id; ?> <?php echo $product->item_name; ?> <?php echo $product->item_price; ?> <?php echo $product->item_quantity; ?> Order Now
[/php]

Here’s item.php

[php]<?php
class Item{
var $item_id;
var $item_name;
var $item_price;
var $item_quantity;
}
?>[/php]

Let’s see,

You don’t have a $_GET variable item_id, you have one that is id. Not does it have an index variable.

Your class does nothing.

Better practice would to be to use public instead of var (IMO) for there are three basic kinds of variables a class has - private, protected and public. I concur your class does nothing. It also is easier to follow and debug. I’m not too familiar with mysqli_ but it looks like you’re trying read in objects instead of a class? I know you can do that with PDO. Maybe someone more experience in mysqli_ statements can help you out better?

Thanks for the replies guys. I’ve modified my code quite a bit, I don’t have any errors, but it does not display the selected item on the web page this time when I click order now. I’m just learning to make a shopping cart so please understand why I’m having these issues. I’ve tried the select statement in the SQL query test in phpMyAdmin and it works correctly but it does not display the item that I have selected when I click order now. Can anyone explain why this did not work and perhaps help me change the code to make it work?

Here’s the new code:

aShopCart.php

[php]<?php
session_start();
require ‘DBConn.php’;
require ‘Item.php’;
//select the test db
mysqli_select_db($DBConnect, “test”);

if(isset($_GET[‘item_id’])){
$result = mysqli_query ($DBConnect, ‘select * from tbl_item where item_id =’.$_GET[‘item_id’]);
$product = mysqli_fetch_object($result);

$item = new Item();
$item->item_id = $product->item_id;
$item->item_name = $product->item_name;
$item->item_price = $product->item_price;
$item->item_quantity =1;
$_SESSION[‘cart’][] = $item;

}
?>

item_id = $product->item_id; $item->item_name = $product->item_name; $item->item_price = $product->item_price; $item->item_quantity =1; $_SESSION['cart'][] = $item;

$cart = unserialize(serialize($_SESSION[‘cart’]));
for($i=0; $i<count($cart); $i++){
?>





</tr>
<?php } }?>
ID Name Price Quantity Sub Total
<?php echo $cart[$i]->item_id; ?> <?php echo $cart[$i]->item_name; ?> <?php echo $cart[$i]->item_quantity; ?> <?php echo $cart[$i]->item_price *$cart[$i]->item_quantity; ?>

Continue Shopping

?>
[/php]

myShop.php(this is the index)

[php]<?php
include ‘DBConn.php’;
require ‘DBConn.php’;

//select the test db
mysqli_select_db($DBConnect, “test”);

$result = mysqli_query($DBConnect, ‘select * from tbl_item’);
?>

<?php while($product = mysqli_fetch_object($result)){?>
	<tr>
		<td><?php echo  $product->item_id; ?></td>
		<td><?php echo  $product->item_name; ?></td>
		<td><?php echo  $product->item_price; ?></td>
		<td><?php echo  $product->item_quantity; ?></td>
		<td><a href="aShopCart.php?id=<?php echo $product->item_id; ?>">Order Now</a></td>
	</tr>
<?php } ?>
		
</table>[/php]

item.php (my variables are var because the YouTube video I followed had it like this and it worked for them)

[php]<?php

class Item{
var $item_id;
var $item_name;
var $item_price;
var $item_quantity;
}

?>
[/php]

ID Name Price Quantity Buy

It probably doesn’t have an item id in the database with a value of ‘’.

Few things,
[php] $result = mysqli_query ($DBConnect, ‘select * from tbl_item where item_id =’.$_GET[‘item_id’]);[/php]
BAD!!! You want prepared statements with parameterized queries and not just drop a variable into a sql statement.

[php]

Order Now[/php]
Based on this line, where do you get your $_GET[‘item_id’] from?

I’m not sure exactly what you mean by it not having the item id with he value of ‘’. in the database but from what I understand, the item_id values that I can see on the page are in the database. Here’s a link for when I click order now, http://localhost/a2/aShopCart.php?id=15917 , that’s the item_id for the product that I have selected.

I understand that it’s better to do it with prepared statements with parameterized queries but for this example that I have found on youtube, https://www.youtube.com/watch?v=lrIz9C0RNXU, this is exactly how it is and I just want to try and make mine work using this code. It’s not going to be used for anything real, I’m just trying to get it to work and understand why it does not.

The $_GET[‘item_id’] comes from a database called “test” in my wamp server.

Here’s the connected code for it: $DBConnect = mysqli_connect(“localhost”,“test”);

$_GET[‘item_id’]

Doesn’t come from your database, it comes from the get global array, typically from a url. You don’t have a value in the url named, item_id. You DO have, ‘id’.

Thank you so much for that explanation, I’ve done made the small change and it worked :smiley: Much appreciated!

I’ve changed up the code now to increase the quantity if the item is selected again but I’m getting the following errors: Trying to get property of non-object in C:\wamp\www\a2\aShopCart.php on line 53/54/55:
[php]

<?php echo $cart[$i]->item_id; ?>
<?php echo $cart[$i]->item_name; ?>
<?php echo $cart[$i]->item_price; ?>
<?php echo $cart[$i]->item_quantity; ?>
<?php echo $cart[$i]->item_price *$cart[$i]->item_quantity; ?>[/php]

Can somebody help me fix it?

I did not get these errors before I added this piece of code:

[php]//check product exists in cart

$index = -1;
$cart = unserialize(serialize($_SESSION[‘cart’]));
for($i=0;$i<count($cart);$i++)

if($cart[$i]->item_id=$_GET['item_id'])
{
	$index = $i;
	break;
}

if($index==-1)
	$_SESSION['cart'][] = $item;
else{
	$cart[$index]->item_quantity++;
	$_SESSION['cart'][] = $cart;
}[/php]

Here’s the full code:
[php]<?php
session_start();
require ‘DBConn.php’;
require ‘Item.php’;
//select the test db
mysqli_select_db($DBConnect, “test”);

?>

item_id = $product->item_id; $item->item_name = $product->item_name; $item->item_price = $product->item_price; $item->item_quantity = 1;

//check product exists in cart

$index = -1;
$cart = unserialize(serialize($_SESSION[‘cart’]));
for($i=0;$i<count($cart);$i++)

if($cart[$i]->item_id=$_GET['item_id'])
{
	$index = $i;
	break;
}

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

$_SESSION[‘cart’][] = $item;
$cart = unserialize(serialize($_SESSION[‘cart’]));
for($i=0; $i<count($cart); $i++){
?>






</tr>
<?php } }?>
ID Name Price Quantity Sub Total
<?php echo $cart[$i]->item_id; ?> <?php echo $cart[$i]->item_name; ?> <?php echo $cart[$i]->item_price; ?> <?php echo $cart[$i]->item_quantity; ?> <?php echo $cart[$i]->item_price *$cart[$i]->item_quantity; ?>

Continue Shopping

?>
[/php]

First, lets update your code to use prepared statements. After that, we will work on the issue.

MySQLi Prepared Statements

I’ve managed to make it work by using prepared statements. Thanks for your help!

Good job

Sponsor our Newsletter | Privacy Policy | Terms of Service