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’] 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:
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:
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 |
Here’s item.php
[php]<?php
class Item{
var $item_id;
var $item_name;
var $item_price;
var $item_quantity;
}
?>[/php]