0

I need help solving this issue. I have stored my pictures as blob in mysql database and when i try to run this code to retrieve data from the database, the pictures are displayed as punch of characters but the rest of data types are working fine. I think the browser does not know that the data is a picture. How can I convert the blob into pictures in this code.

<?php
    include_once('db.php');

    if(isset($_POST['personID'], $_POST['fName'], $_POST['lName'], $_POST['title'], $_POST['pic']))
    {
      $personID= $_POST['personID'];
      $fName= $_POST['fName'];
      $lName= $_POST['lName'];
      $title= $_POST['title'];
      $pic= $_POST['pic'];

      if(mysql_query("INSERT INTO samidb.persons VALUES ('$personID','$fName', '$lName', '$title',     '$pic')"))
        echo "Successful Insertion!";
      else
        echo "Please try again";
    }


$res = mysql_query("SELECT * FROM samidb.persons");

?>
<html>
<head>

<style type="text/css">
 li { list-style-type: none; display: inline-block; padding: 10px; text-align: center;}
//li:hover { background-color: yellow; }
</style>
</head>
<body>
<form action="." method="POST">
person ID: <input type="text" name="personID"/><br />
First Name: <input type="text" name="fName"/><br />
Last Name: <input type="text" name="lName"/><br />
title: <input type="text" name="title"/><br />
pic: <input type="image" name="pic"/><br />
<input type="submit" value=" Enter "/>
</form>

<h1>List from database ..</h1>
<ul>
<?php
    while( $row = mysql_fetch_array($res) )
    echo "<li>$row[personID]. <li>$row[fName]  <li>$row[pic]</li> 
            <li><a href='edit.php?edit=$row[personID]'>edit</a></li>
            <li><a href='delete.php?del=$row[personID]'>delete</a></li><br />";
?>
</ul>
</body>
</html>
  • 1) read about sql injections 2) you code does not show how you are trying to output images. One image + content-type per http request. Output of images can not be mixed with output of the text. – Cheery Oct 12 '14 at 21:52
  • there's a reason we suggest not storeing images in the db. –  Oct 12 '14 at 21:52
  • much better to store the images path – Halfpint Oct 12 '14 at 22:05
  • I don't see how this is supposed to work. Normally if you're uploading files to a PHP script I'd expect to see PHP referring to the `$_FILES` array but there's none here. I'm assuming you think that the image will appear in `$row['pic']`, and assuming that it does (I'm pretty sure it doesn't) you need to provide the browser with more information. Either convert it to a Base64-encoded data URL, or insert a URL that's understood by the server to deliver the proper format. Either way, there's more wrong here than can be fixed in a short, succinct answer. –  Oct 12 '14 at 22:05

1 Answers1

0

Most people just store the path to the image. It's easy, it puts the saving of the data in the file system, built for that sort of thing, it cuts down on the db size, etc.

However, you should be able to use it as an inline image, usually base 64 encoded, as mentioned here:

Embedding Base64 Images

FYI, I tested this out using this code:

<img src="data:image/png;base64,<?php echo base64_encode (file_get_contents('./screenshot.png')); ?>" />

You could probably do something like:

<img src="data:image/png;base64,<?php echo base64_encode ($row['pic']); ?>" />
Community
  • 1
  • 1
Hans
  • 3,403
  • 3
  • 28
  • 33