/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Undefined Index help

Hey!

On my homepage I want to click on the kitchens tab to display all products with the category ‘kitchens’ only to be displayed. On this kitchen page I want to display the data and to link to further info on that product.

When I click this link this error occurs;

[COLOR=”Red”]Notice: Undefined index: product_id in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_t006172/public_html/viewproduct.php on line 46[/COLOR]

The problem is on this page; [url]http://www.numyspace.co.uk/~unn_t006172/kitchens.php[/url]

[COLOR=”Red”]kitchens.php[/COLOR]

<?php

//connect to database
require_once (‘allsorts_dbconnect.php’);

//make a query to show all products with category = “kitchens”
$categorySQL = “select product_id, product_name, product_category, product_price from tblproducts where product_category = ‘Kitchen'”;
//execute that query
$rsCategories = mysql_query ( $categorySQL );
//display the start of the table
echo “<table border=”1″>n”;
//loop around the record set displaying each product on a new row
while($category = mysql_fetch_array( $rsCategories )){
$product_id = $category [‘product_id’];
$product_name = $category [‘product_name’];
$product_category = $category [‘product_category’];
$product_price = $category [‘product_price’];
echo “<tr><td>$product_id</td><td>$product_name</td><td>$product_price</td><td><a href=”viewproduct.php?product_id = $product_id”>read more…</a></td></tr>n”;

}//while
//end the table
echo “</table>n”;

?>

[COLOR=”Red”]viewproduct.php[/COLOR]

<?php
// connect to the database
require_once (‘allsorts_dbconnect.php’);
// get the product_id from request stream
$product_id = $_REQUEST [‘product_id’];
// use that code in an sql statement to retrieve further details for that product
$productSQL = “select product_id, product_name, product_description, product_price from tblproducts where product_id = ‘$product_id'”;
// execute the sql query
$rsProduct = mysql_query($productSQL);
// get the records from the result set into variables
$product = mysql_fetch_array($rsProduct);
$product_id = $product [‘product_id’];
$product_name = $product [‘product_name’];
$product_description = $product [‘product_description’];
$product_price = $product [‘product_price’];

echo “<tr><td>$product_name</td><td>$product_description</td><td>$product_price</td></tr>n”;
// end the table
echo “</table>n”;

?>

Thanks in advance

Tom

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@eval_BadCode_Feb 03.2011 — Undefined Index = ArrayIndexOutOfBoundsException

ArrayIndexOutOfBoundsException extends RuntimeException {}

throw new ArrayIndexOutOfBoundsException PHP does not.

first you should:
[code=php]
echo "<pre>", var_dump($product), "</pre>";
[/code]


then you should:
[code=php]

<?php
require_once ('allsorts_dbconnect.php');

$product_id = mysql_real_escape_string($_REQUEST['product_id']);

$rsProduct = mysql_query("
select product_id, product_name, product_description, product_price
from tblproducts
where product_id = '$product_id'");

echo mysql_errno($dBcon), ": ", mysql_error($dBcon);

$product = mysql_fetch_object($rsProduct, "Product");

class Product {
private $product_id;
private $product_name;
private $product_description;
private $product_prince;

//null constr

public tableMarkup() {
return sprintf("<table><tr><td>%s</td><td>%s</td><td>%s</td></tr></table>",
html_entities($this->product_name),
html_entities($this->product_description),
html_entities($this->product_prince) );
}
}

echo $product->tableMarkup();

?>
[/code]


Why doesn't your product_ID exist? my guess is your query has an error, no clue...


Edit:

It's because you have a space in your URL like this:
[CODE]viewproduct.php?product_id = AA101[/CODE]
the page works fine when I write it like this:
[CODE]viewproduct.php?product_id=AA101[/CODE]
Copy linkTweet thisAlerts:
@kemp667authorFeb 04.2011 — Your a lifesaver.

Thank you very much.
Copy linkTweet thisAlerts:
@kemp667authorFeb 04.2011 — Thanks
×

Success!

Help @kemp667 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...