-1

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

So Im back with another php error, that I cant solve. But Im getting better:)

I have created a simple script that stores images in a database, I have no problems to store the file, but when Im reading the file i get an index error. It says

Notice: Undefined index: id in C:\wamp\www\gallery2\show.php on line 13

I cant really get what the problem is since Im thinking that everything is correct !?

the code for showing the images are

<?php

$username = "root";
$password = "";
$host = "localhost";
$database = "guestbook";


@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$id = $_GET['id'];

if(!isset($id) || empty($id)){
die("Please select your image!");
}else{

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];

header('Content-type: image/jpg');
echo $content;

}

?>
Community
  • 1
  • 1
Dymond
  • 2,158
  • 7
  • 45
  • 80

3 Answers3

1

You are trying to access $_GET['id']. However, if no argument id is present in the querystring of the request, the index id will not be available in the $_GET superglobal. That's why you receive the notice.

So you should be doing something like this:

$id = !empty( $_GET[ 'id' ] ) ? $_GET[ 'id' ] : null;
Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
  • That helped me to ge rid of the error, but I cant accualy read from the database. do you have a clue how to displat the images in the database ? thanks you. – Dymond Oct 30 '11 at 13:54
0

It is telling you the value id does not exist in the $_GET array. Does the URL you are accessing have a ?id=something on it?

Patrick
  • 685
  • 5
  • 12
0

The variable...

$_GET['id'];

...doesn't contain a value, therefore it is not defined.

Check if your form mechanism leading to this $_GET is working, maybe var_dump it.

If your script should cope with an empty variable there, maybe check this variable first, something like this:

 $id = "";
 if (isset($_GET['id']) { $id = $_GET['id']; }
Bjoern
  • 15,934
  • 4
  • 43
  • 48