0

I keep getting a notice saying Undefined index: id. Also, the code does not do what it's meant to do. Its supposed to get data from the database where the ItemID = ID. But the page just ends up blank.

Help please, what can I do?

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
  <link rel="stylesheet" href="my.css">
  <title>Search result</title></head>
<body >
<div id = "header"> <h2> Descriptions and Images</h2></div>
<div id = "main">
session_start();
include(connect.php);
 $id = isset($_GET['id']) ? $_GET['id'] : '';
//$id= $_GET['id'];
//$id = ''; 
//f( !isset( $_GET['id'])) {
   // $id = $_GET['id']; } 
//if ($id == ""){
//  echo"eree";
//}
echo $id;
$query = mysql_query('SELECT * FROM PHP_Item WHERE ItemID = "$id"');
//$row = mysql_fetch_array($query);
while ($row = mysql_fetch_array($query)){
    $Name = $row['Name'];
    $Price = $row['Price'];
    $Description = $row['Description'];
    $Image = $row['Image'];
    echo "<br /><br/>";
    echo "Name: " . $Name;
    echo "<br/>";
    echo "Price: " . $Price;
    echo "<br/>";
    echo "Description: ".$Description;
    echo "<br/>";
    echo "<img src = '".$Image."' alt = '".$Image."'></img>";
    echo"<br/><br/>";
}
?>
</div>
<div id = "footer">
<a href = "logout.php">Logout</a></div>
</html>
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
josh
  • 19
  • 1
  • 2
  • From where you are passing this id. first check your url is it contains id in url string. – Code Lღver Apr 28 '15 at 13:28
  • to show errors (the ugly way) do ini_set('display_errors', TRUE); Also is $id = isset($_GET['id']) ? $_GET['id'] : ''; not working? – sitilge Apr 28 '15 at 13:28
  • Instead of using: $id = isset($_GET['id']) ? $_GET['id'] : ''; Ensure the value is set to a default ID. Maybe: $id = isset($_GET['id']) ? $_GET['id'] : 1; However, it is probably best to direct the user to 404 error page. – Praval 'Shaun' Tirubeni Apr 28 '15 at 13:30
  • you're outputting before header but masking errors that PHP is throwing out at you in the background. – Funk Forty Niner Apr 28 '15 at 13:31
  • Where is ID being sent from? When you get to the page, do you see your ID in your URL at the top? That's a good place to begin. If it's not even relaying your ID over to this page, then you'll never have it in this manner. – Fata1Err0r Apr 28 '15 at 13:32
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – John Conde Apr 28 '15 at 13:34
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – deceze Apr 28 '15 at 13:36

1 Answers1

2

You have forgot to add the starting of PHP tag. Include that here:

<?php //add this tag
session_start();
include(connect.php);

And to show the error add the ini_set in your file on top:

<?php ini_set('display_errors', true); ?>

Hope this will resolve the issue.

UPDATE:

If you are not passing id from URL parameter then replace $_GET with $_REQUEST. You can try this approach also. $_REQUEST can get both GET and POST values.


Footnotes:

You're probably outputting before header (see link below) but masking errors that PHP is throwing out at you in the background.

Using error reporting will tell you.

If that is the case, then you will need to place session_start(); at the top of your code, followed by the rest of your code.

I.e.:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
?>

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
  <link rel="stylesheet" href="my.css">
  <title>Search result</title></head>
<body >
<div id = "header"> <h2> Descriptions and Images</h2></div>
<div id = "main">
<?php 
include(connect.php);

// rest of your code
Community
  • 1
  • 1
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
  • good catch and let's hope that wasn't a bad paste. However, as I stated in a comment under their question, they're most likely outputting before header but masking errors that PHP is throwing out at them in the background. – Funk Forty Niner Apr 28 '15 at 13:32
  • Agreed. I noticed it, but considering some of the other formatting, I just assumed he accidentally dropped it when pasting over, considering it wouldn't echo "undefined" to him if he didn't. – Fata1Err0r Apr 28 '15 at 13:34
  • @Fred-ii- hope this was bad paste ... if not then it will obviously point at right direction. – Code Lღver Apr 28 '15 at 13:34
  • *Aye*, yeah, well the OP's being pointed in the right direction for sure ;-) – Funk Forty Niner Apr 28 '15 at 13:35
  • @Fred-ii- its open community anyone can edit and can improve and post. By the way thanks for editing :). – Code Lღver Apr 28 '15 at 13:46