I am trying to create a shopping cart using PHP and MySQL. I am trying to add something to my cart and an error keeps displaying:
Warning: Undefined array key "id" in C:\Users\Decla\OneDrive\Documents\UniServerZ\www\Shopping_Cart\index.php on line 131
Warning: Undefined array key "name" in C:\Users\Decla\OneDrive\Documents\UniServerZ\www\Shopping_Cart\index.php on line 132
Warning: Undefined array key "price" in C:\Users\Decla\OneDrive\Documents\UniServerZ\www\Shopping_Cart\index.php on line 133
Warning: Undefined array key "quantity" in C:\Users\Decla\OneDrive\Documents\UniServerZ\www\Shopping_Cart\index.php on line 134
Warning: Undefined array key "price" in C:\Users\Decla\OneDrive\Documents\UniServerZ\www\Shopping_Cart\index.php on line 135
Warning: Undefined array key "quantity" in C:\Users\Decla\OneDrive\Documents\UniServerZ\www\Shopping_Cart\index.php on line 135
Warning: Undefined array key "id" in C:\Users\Decla\OneDrive\Documents\UniServerZ\www\Shopping_Cart\index.php on line 137
Warning: Undefined array key "quantity" in C:\Users\Decla\OneDrive\Documents\UniServerZ\www\Shopping_Cart\index.php on line 144
Warning: Undefined array key "price" in C:\Users\Decla\OneDrive\Documents\UniServerZ\www\Shopping_Cart\index.php on line 144
I am expecting the item to be displayed in the "Item Selected table" along with: ID,Item Name,Item Price Item Quantity and Total Price.
The full source code below:
Shopping Cart
<?php
session_start();
$connect = mysqli_connect("localhost", "root", "root", "shopping_cart");
if (isset($_GET['add_to_cart']))
if(!isset($_SESSION['Cart'])) {
$_SESSION['Cart'] = array();
}
if (isset($_SESSION['Cart'])) {
$session_array_id = array_column($_SESSION['Cart'], "id");
if (isset($_GET['id'], $session_array_id)) {
$session_array = array(
'ID' => $_GET['id'],
"Name" => $_GET['name'],
"Price" => $_GET['price'],
"Quantity" => $_GET['quantity']
);
$_SESSION['Cart'][] = $session_array;
}
}else{
// $_SESSION['Cart']
}
if ($connect->connect_error) {
die("Connection failed: " . $connect->connect_error);
}
echo "Connected successfully";
?>
<!DOCTYPE html>
<html>
<head>
<title>Shopping Cart</title>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container-fluid">
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<h2 class="text-center">Shopping Cart Date</h2>
<div class="col-md-12">
<div class="row">
<?php
$query = "SELECT * FROM cart_item";
$result = mysqli_query($connect,$query);
while ($row = mysqli_fetch_array($result)) {?>
<div class="col-md-4">
<form method="get" action="index.php">
<img src="img/<?php echo $row['image']; ?>" style='height: 150px;'>
<h5 class="text-align"><?php echo $row['name']; ?></h5>
<h5 class="text-align">$<?php echo number_format($row['price'],2); ?></h5>
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<input type="hidden" name="name" value="<?php echo $row['name']; ?>">
<input type="hidden" name="price" value="<?php echo $row['price']; ?>">
<input type="number" name="quantity" value="1" class="form-control">
<input type="submit" name="add_to_cart" class="btn btn-warning btn-block my-2" value="Add To Cart">
</form>
</div>
<?php }
?>
</div>
</div>
</div>
<div class="col-md-6">
<h2 class="text-center">Item Selected</h2>
<?php
$total = 0;
$output = "";
$output .= "
<table class='table table-bordered table-striped'>
<tr>
<th>ID</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Item Quantity</th>
<th>Total Price</th>
<th>Action</th>
</tr>
";
if (!empty($_SESSION['Cart'])) {
foreach ($_SESSION['Cart'] as $key => $value) {
$output .= "
<tr>
<td>".$value['id']."</td>
<td>".$value['name']."</td>
<td>".$value['price']."</td>
<td>".$value['quantity']."</td>
<td>".number_format($value['price'] * $value['quantity'])."</td>
<td>
<a href='index.php?action=remove&id=".$value['id']."'>
<button class='btn btn-danger btn-block'>Remove</button>
</a>
</td>
";
$total = $total + $value['quantity'] * $value['price'];
}
$output .= "
<tr>
<td colspan='3'></td>
<td><b>Total Price</b></td>
<td>".number_format($total,2)."</td>
<td>
<a href='index.php?action=clearall'>
<button class='btn btn-warning btn-block'>Clear All</button>
</a>
</td>
</tr>
";
}
echo $output;
?>
</div>
</div>
</div>
</div>
<?php
if (isset($_GET['action'])) {
if ($_GET['action'] == "clearall") {
unset($_SESSION['Cart']);
}
if ($_GET['action'] == "remove") {
foreach ($_SESSION['Cart'] as $key => $value) {
if ($value['id'] == $_GET['id']) {
unset($_SESSION['Cart'][$key]);
}
}
}
}
?>
</body>
</html>