image stored in database can't be displayed

I put together the following blocs of code for uploading pictures into a database and displaying them on a webpage. The pictures are supposed to be displayed on the member’s only page of a website I’m working on, upon logging in, and they are supposed to be the member’s uploaded picture. I created several members and and used one of my existing member accounts to test the uploading process. The picture upload process appeared to have been successful when I checked on myphpadmin. Yet, when I login with this account, no picture is displayed, instead, a tiny jpg icon is displayed at the top left corner of the box in which the picture was supposed to be displayed. Same thing when I login with the other accounts with which I haven’t yet uploaded a picture.

I’ll start with the code that installs the table in the database
[php]$query = "CREATE TABLE images (

member_id INT UNSIGNED,
image_name varchar(255) NOT NULL,
image_type varchar(4) NOT NULL,
image_size int(8) NOT NULL,
image_cartegory VARCHAR(20) NOT NULL,
image_path VARCHAR(300),
image_date DATE


Then here is the code which allows the member to upload his picture:


And here is the insertimage.php which inserts the image into our database: Note that I have to authenticate the user in order to register his session id which is used later on in the select query to identify him and select the right image that corresponds to him.


//This file inserts the main image into the images table.

//address error handling

ini_set (‘display_errors’, 1);
error_reporting (E_ALL & ~E_NOTICE);

//authenticate user
//Start session

    //Connect to database

require (‘config.php’);

//Check whether the session variable id is present or not. If not, deny access.
if(!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) {
	header("location: access_denied.php");


// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES[‘image’]) && $_FILES[‘image’][‘size’] > 0) {

  // Temporary file name stored on the server
  $tmpName  = $_FILES['image']['tmp_name'];  
  // Read the file 
  $fp      = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));
  $data = addslashes($data);

  // Create the query and insert
  // into our database.
  $query = "INSERT INTO images (member_id, image_cartegory, image_date, image) VALUES ('{$_SESSION['id']}', 'main', NOW(), '$data')";
  $results = mysql_query($query);
  // Print results
  print "Thank you, your file has been uploaded.";

else {
print “No image selected/uploaded”;

// Close our MySQL Link

} //End of if statmemnt.

?> [/php]

On the page which is supposed to display the image upon login in, I inserted the following html code in the div that’s supposed to contain the image:

                <div id="image_box" style="float:left; background-color: #c0c0c0; height:150px; width:140px; border-           color:#a0a0a0;border-style:outset;border-width:1px; margin:auto; "> 
                     <img src=picscript.php?imname=potwoods>

And finally, the picscript.php contained the select query:

                         //address error handling

                         ini_set ('display_errors', 1);
                         error_reporting (E_ALL & ~E_NOTICE);

                         //include the config file

                         $image = stripslashes($_REQUEST[imname]);
                         $rs = mysql_query("SELECT* FROM images WHERE member_id = '".$_SESSION['id']."' AND cartegoty = 'main' ");
                         $row = mysql_fetch_assoc($rs);
                         $imagebytes = $row[image];
                         header("Content-type: image/jpeg");
                         print $imagebytes;


Now the million dollar question is, “What is preventing the picture from getting displayed?” I know this is a very lengthy and laborious problem to follow but I’m sure there is someone out there who can point out where I’m not getting it. Thanks.

are these two table names supposed to be spelt like this?

$query = "INSERT INTO images (member_id, image_cartegory, image_date, image) VALUES

$rs = mysql_query(“SELECT* FROM images WHERE member_id = '”.$_SESSION[‘id’]."’ AND cartegoty = ‘main’ ");

i notice one ends in ‘ry’ while the other ends in ‘ty’ so i’m thinking maybe you have a typo?

thanks redscouse, that was the problem. now my image gets displayed. thanks again.

your welcome, glad to of helped :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service