Updating Images in MySQL with PHP
<p>I am on my way to create the feature for users to upload their profile photo to the database. I manage to upload my photo to database in binary form (BLOB) but I am having problem while trying to display it.</p>

<p>upload form code:</p>

<pre><code>&lt;?php //get the posted image when the submit button is clicked 
$username = "MentorMenteeData"; 
$password = "mentormenteedata"; 
$host = "localhost"; 
$database = "mentormenteesystem"; 

// Make the connect to MySQL or die 
// and display an error. 
$link = mysql_connect($host, $username, $password); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 

// Select your database 
mysql_select_db ($database);         

   if (isset($_FILES['image']) &amp;&amp; $_FILES['image']['size'] &gt; 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); 
      fclose($fp); 

      $student_id=$row_student['student_id']; 

      // Create the query and insert 
      // into our database. 
      $query = "UPDATE student SET student_img='$data' WHERE student_id ='$student_id'"; 
      $query .= "(image) VALUES ('$data')"; 
      $results = mysql_query($query, $link); 

      // Print results 
      print "Thank you, your file has been uploaded."; 

} 
else { 
   print "No image selected/uploaded"; 
} 

// Close our MySQL Link 
mysql_close($link); 
?&gt; 

&lt;form action="" method="post" enctype="multipart/form-data" name="changer"&gt; 

&lt;strong style="color: #FFD700;"&gt;Upload your image:&lt;/strong&gt;&lt;br /&gt; 
&lt;input name="MAX_FILE_SIZE" value="102400" type="hidden"&gt;&lt;br /&gt;&lt;br /&gt; 
&lt;input namge="image" accept="image/jpeg" type="file"&gt; 
&lt;input type="submit" value="Submit"&gt; 
&lt;/form&gt;
</code></pre>

<p>Code to display image:</p>

<pre><code>&lt;?php 

$username = "MentorMenteeData"; 
$password = "mentormenteedata"; 
$host = "localhost"; 
$database = "mentormenteesystem"; 

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 = $_REQUEST['student_id']; 

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

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

header('Content-type: image/jpeg'); 
     echo $content; 
?&gt;
</code></pre>

<p>I could see my database table for the image column containing some bits but I just cant seems to display it. Please advise.</p>
