Ringtones - Favorite Movie - Problem Mortgage - Mortgages - Online Advertising

PDA

View Full Version : PHP Shopping basket - undefined index


siyah miyah
Apr 28th 2008, 7:07 am
This is my cart.php file with the error:

Notice: Undefined index: prod_id in /**********************/*****/****/*******/******/******/web/fsbasket/cart.php on line 20

prod_id is the reference to the column in my database....so in my query I am calling on the prod_id for the shopping basket....Can someone tell me why I get this msg?

Is there a problem with connecting to the database perhaps?

<?php
// Include MySQL class
include("mysql.class.php");
// Include database connection
include("global.inc");
// Include functions
include("functions.inc");
// Start the session

session_start();

// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
if ($cart) {
$cart .= ','.$_GET['prod_id'];
} else {
$cart = $_GET['prod_id'];
}
break;
case 'delete':
if ($cart) {
$items = explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($_GET['prod_id'] != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
$cart = $newcart;
}
break;
case 'update':
if ($cart) {
$newcart = '';
foreach ($_POST as $key=>$value) {
if (stristr($key,'qty')) {
$prod_id = str_replace('qty','',$key);
$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($prod_id != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
for ($i=1;$i<=$value;$i++) {
if ($newcart != '') {
$newcart .= ','.$prod_id;
} else {
$newcart = $prod_id;
}
}
}
}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Shopping Basket</title>
<link rel="stylesheet" href="styles.css" />
</head>

<body>

<div id="shoppingcart">

<h1>Your Shopping Basket</h1>

<?php
echo writeShoppingCart();
?>

</div>

<div id="contents">

<h1>Please check quantities</h1>

<?php
echo showCart();
?>

<p><a href="index.php">Return to homepage</a></p>

</div>

</body>
</html>

Thanks, in advance, for any responses!

xrvel
Apr 28th 2008, 7:12 am
Its a notice. You can turn it off.
Put this in top of your PHP script.

error_reporting(E_ALL ^ E_NOTICE);
Or when you try to get something from post / get, use this :

$name = '';

//
// You want to read it from $_GET
//
if (isset($_GET['name'])) {
$name = $_GET['name'];
}

siyah miyah
Apr 28th 2008, 9:10 am
Thank you for your response.

What it really comes down to is..when I try pull data from the database i set them in the code (illustrated below):

$output[] = '<tr>';
$output[] = '<td><form action="cart.php?action=delete&prod_id=.'$id.'" method="post" id="Delete">Remove</a></td>';
$output[] = '<td>'$contents['p_name']'</td>'; //'.$p_name.'
$output[] = '<td>&pound;'.$price_wk.'</td>';
$output[] = '<td><input type="text" name="qty '.$id.' "value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = "<td>&pound;".($price_wk * $qty)."</td>";
$total += $price_wk * $qty;
$output[] = '</tr>';

I get an undefined variable error. Which relates to $..which is the name of the attribute I am trying to extract from the database....

Must I define these variables. I thought that since it was coming from the database it was not necessary. PLEASE correct me.

If so, would I just have to define it as stated above using the $name=''; and if(isset)? I had tried this earlier, setting it in my cart.php file and it still did not work...should it be in the functions.inc file?

Synchronium
Apr 28th 2008, 9:18 am
Check your DB column names.

siyah miyah
Apr 28th 2008, 9:25 am
Thanks.

DB column names are exactly as stated.

Could it be a problem with my DB connection?

siyah miyah
Apr 28th 2008, 10:33 am
I use action to refer to the delete, add items, etc. This can be seen in full in the first post. I keep getting the error:

Notice: Undefined index: action in /*************************************************fsbasket/cart.php on line 67

But hasn't this already been defined with the $action = $_GET['action']; ?

Also for this line $result = $db->query($sql);
I get this error:
Fatal error: Call to a member function query() on a non-object in /*****************************************fsbasket/cart.php on line 37

What does this mean and how do I fix it?