sansam
sansam

Reputation: 57

Unable to display image from MySQL table

I'm searching for a resolution for displaying image which is saved in MySQL table. I have read almost all the previous questions in this site. Have followed them, but no luck.

I am able to save the files all right. When trying to display, its only showing the binary codes.

Have spend several days to do it myself, but couldn't find the mistake in program.

The aim is to display the users Name, last name, date of birth and image using 3 different tables, and then change them if the user wants. All the data are displayed fine and able to change as well. But cant display image..!!

Have tried to 'echo' the image in line 22 for testing and then in line 45 where is should display. Both ways its just the binary codes. Any help/suggestions will be highly appreciated..

<?php
session_start();

include 'db_connect.php';

$user   = (isset($_POST['user']) && !empty($_POST['user']) ? $_POST['user'] :"");
$pass   = (isset($_POST['pass']) && !empty($_POST['pass']) ? $_POST['pass'] :"");   

$sql = mysqli_query($connect, "SELECT mli.cust_id, mm.fname, mm.lname, mm.cust_dob, mi.cust_img 
          FROM member_login_info mli 
          LEFT JOIN member_master mm   ON mli.cust_id = mm.cust_id
          LEFT JOIN member_img mi  ON mli.cust_id = mi.cust_id
          WHERE mli.profilename = '$user' AND mli.password = sha1('$pass') ");        

$result = mysqli_fetch_array($sql);

  $fname  = $result["fname"];
  $lname  = $result["lname"];
  $dob    = date("d/m/Y", strtotime($result["cust_dob"]));
  $_SESSION["cid"] = $result["cust_id"];
  $image = $result["cust_img"];
  echo "<img src='" .$result['cust_img']. "'>"
?>

<div id="edit_data"> 
 <fieldset style="width:30%">
    <legend>Edit information</legend> 

 <table border="0">
    <form name="edit" enctype="multipart/form-data" method="POST" action="test_edit1.php">
    <tr>        
    <td>First Name:</td>
    <td><input name="fname" type="text" id="fname" value="<?php echo $fname; ?>" > </td> 
    </tr>   
    <tr> 
    <td>Last Name:</td> 
    <td><input name="lname" type="text" id="lname" value="<?php echo $lname; ?>"> </td> 
    </tr>   
    <tr> 
    <td>Date of Birth:</td><td> 
    <input name="dob" type="text" id="dob" value="<?php echo $dob; ?>"> </td>
    <input name="cid" value="<?php echo $cid; ?>" type="hidden" />
    </tr>   
    <tr> 
    <td>Photo:</td> <td> <?php echo "<img src='".$result['cust_img']."'/>" ?> </td>
    </tr>

    <tr> 
    <td>Upload your profile picture:</td><td> <input name="image" type="file"> </td>
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
    </tr> 

    <tr> 
    <td><input id="exit" type="submit" name="exit" value="Exit" onclick="window.location.href='display.html'"></td>
    <td><input id="edit" type="submit" name="edit" value="Save"></td>  
    </tr> 
    </form> 
 </table>

Upvotes: 1

Views: 315

Answers (1)

n-dru
n-dru

Reputation: 9420

I assume that you don't store a path to that image, but the image content itself and that it is jpg (you can change to png or whatever type you have there). If so, use data URI syntax for src:

$src = "data:image/jpg;base64,".base64_encode($result['cust_img']);
echo "<img src='$src'>";

Upvotes: 1

Related Questions