Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-17-2012, 12:50 PM   PM User | #1
fightapilotdean
New Coder

 
Join Date: Mar 2012
Location: Leicester
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
fightapilotdean is an unknown quantity at this point
Question Help with shopping cart, PHP code

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
fightapilotdean is offline   Reply With Quote
Old 09-17-2012, 11:55 PM   PM User | #2
nevenne
New to the CF scene

 
Join Date: Sep 2012
Location: serbia
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
nevenne is an unknown quantity at this point
l can make all script for you
nevenne is offline   Reply With Quote
Old 09-18-2012, 12:23 AM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,496
Thanks: 44
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by fightapilotdean View Post
"Notice: Undefined index: quantity- in C:\xampp\htdocs\mySite\cart.php on line 9"

PHP Code:
<?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){
Your $key variable contains no value. Look at the error message - your index in the $_POST array doesn't exist. Sure, you might have $_POST['quantity-1'], $_POST['quantity-2'], $_POST['quantity-3'] etc but where is your $_POST['quantity-'] ? - Chances are there isn't one.

Something is wrong with $key. As you're generating your form dynamically, you need to show us the outputted html form for us to have a better understanding of the problem here. It could be that your form isn't being output correctly or that you're not processing it correctly. Either way, going for a generic value of quantity-X where X is a key from the post array is asking for trouble - you're in essence looking for a index in your array with every form field name such as quanitity-submit (yup, thats right, you're even testing for the submit button in your code).

A better way would be:
PHP Code:
print "<input type=\"text\" name=\"quantity[$row[id_products]]\" value=\"\">" 
The [] means its an array. You then process it in PHP like:
PHP Code:
<?php
foreach ($_POST['quantity'] as $key => $value//$key is the $row['id_products'] from your db
?>
Finally, there is a sticky topic in the main forum about using [php] and [/php] tags. It applies to everyone here and its considered a bit rude to post many lines of code as you have and ask us to fix it without using them. You have 13 posts now so you've had time to read the sticky so please act on it! The php tags have the effect you've seen in my reply and make it a lot easier for us to read your code. Pretty please would you use them in the future?
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 09-18-2012 at 12:27 AM..
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
fightapilotdean (09-18-2012)
Reply

Bookmarks

Tags
php, shopping cart, tutorial

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:19 PM.


Advertisement
Log in to turn off these ads.