Hi, i have been following a tutorial to create a php/mysql shopping cart by TheTutSpace on youtube and i have come accross an error with the script. When i run it on my page i get
"Notice: Undefined index: quantity- in C:\xampp\htdocs\mySite\cart.php on line 9"
Code for cart.php :
<?php
if(isset($_POST['submit'])){
foreach($_POST as $key => $value){
$key = explode("-",$key);
$key = end($key);
$key = explode("submit",$key);
$key = end($key);
if($_POST['quantity-'.$key] == 0){
unset($_SESSION['cart'][$key]);
echo "Unset session";
} else {
$_SESSION['cart'][$key]['quantity'] = $value;
echo "Updated session";
}
}
} error_reporting(0);
?>
<h1>View Cart</h1>
<a href="index.php?page=products" title="Go back to products page">Go back to products page</a>
<?php $sql = "SELECT * FROM products WHERE id_products IN (";
foreach($_SESSION['cart'] as $id => $value){
$sql .= $id . ",";
}
$sql = substr($sql,0,-1).") ORDER BY name ASC";
$query = mysql_query($sql);
if(empty($query)){
echo "<br /><span class='i'>You need to add an item before you can view it here</span>";
}
?>
<form method="post" action="index.php?page=cart">
<fieldset>
<table>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Price per item</th>
<th>Total Cost</th>
</tr>
<?php
$sql = "SELECT * FROM products WHERE id_products IN (";
foreach ($_SESSION['cart'] as $id => $value){
$sql .= $id . ",";
}
$sql = substr($sql,0,-1).") ORDER BY name ASC";
$query = mysql_query($sql);
$total_price = 0;
if(!empty($query)){
while($row = mysql_fetch_array($query)){
$subtotal = $_SESSION['cart'][$row['id_products']]['products']*$row['price'];
$total_price += $subtotal;
?>
<tr>
<td><?php echo $row['name'];?></td>
<td><input type="text" name="quantity-<?php echo $row['id_products'];?>" size="5" value="<?php echo $_SESSION['cart'][$row['id_products']]['quantity'];?>" /></td>
<td><?php echo "£" . $row['price'];?></td>
<td><?php echo "£" . $_SESSION['cart'][$row['id_products']]['quantity']*$row['price'];?></td>
</tr>
<?php
} }
?>
<tr>
<td> </td>
<td></td>
<td>Total Price</td>
<td><?php echo "£" . $total_price;?></td>
</tr>
</table>
<input type="submit" name="submit" value="Update Cart" /></fieldset>
</form>
<p> To remove an item, set quantity to 0</p>
if relevant code for index.php:
<?php
session_start();
require_once("includes/connect.php");
if(isset($_GET['page'])){
$pages = array("products","cart");
if(in_array($_GET['page'],$pages)){
$page = $_GET['page'];
} else {
$page = "products";
}
} else {
$page = "products";
}
?>
<html>
<head>
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/style.css" />
<title>Shopping Cart</title>
</head>
<body>
<div id="container">
<div id="main"><?php require($page . ".php"); ?></div>
<div id="sidebar"><h1>Cart</h1>
<?php
if(isset($_SESSION['cart'])){
$sql = "SELECT * FROM products WHERE id_products IN (";
foreach($_SESSION['cart'] as $id => $value){
$sql .= $id. ",";
}
$sql = substr($sql,0,-1) . ") ORDER BY id_products ASC";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
?>
<p><?php echo $row['name']; ?><?php echo " " . $_SESSION['cart'][$row['id_products']]['quantity']; ?></p>
<a href="index.php?page=cart">Got to cart</a>
<?php
}
} else {
echo "<p>Your cart is empty. <br />Please add some products</p>";
}
?></div>
</div>
</body>
</html>
if anyone could give me some pointers as to which bit is wrong it would be greatly appreciated. with the video tutorials i got a little confused when it came to the updates that were made. i have tried restarting several times and i seem to get the same error everytime
thanks for any help in advance