-2

I've been trying to display the image I uploaded into the db using a simple insertform. I've only been able to retrieve the image-name into the "data-file", what I'm wondering is how can I easily display the image in the same form location instead of the image-name?

enter image description here

This is my result at this moment, as you can see I've only added an image to one of the inserts, and I would like to change the "footer.jpg-name" with the image displayed here.

<html>
<head>
</head>
<body>
<form action="insertform.php" method="post">
Topic: <input type="text" name="topic"><br />
Name: <input type="text" name="name"><br />
Attendance: <input type="text" name="attendance"><br />
Image: <input type="file" name="image"><br />
<input type="submit" name="submit">
</form>


<?php
if (isset($_POST['submit'])){

$con = mysql_connect("localhost","username","password");

if (!$con){
    die("Can not connect: " . mysql_error());
}

mysql_select_db("testingisfun",$con);

$sql = "INSERT INTO Lectures (Topic,Name,Attendance,Image) VALUES ('$_POST[topic]', '$_POST[name]', '$_POST[attendance]', '$_POST[image]')";

mysql_query($sql,$con);

mysql_close($con);
}
?>

</body>
</html>

Heres the "data-file".

<html>
<head>
</head>
<body>
<?php
$con = mysql_connect("localhost","username","password");
if (!$con){
    die("Can not connect: " . mysql_error());
}
mysql_select_db("testingisfun",$con);
$sql = "SELECT * FROM lectures";
$myData = mysql_query($sql,$con);
echo "<table border = 1>
<tr>
<th>Topic</th>
<th>Name</th>
<th>Attendance</th>
<th>Image</th>
</tr>";
while($record = mysql_fetch_array($myData)){
    echo "<tr>";
    echo "<td>" . $record['Topic'] . "</td>";
    echo "<td>" . $record['Name'] . "</td>";
    echo "<td>" . $record['Attendance'] . "</td>";
    echo "<td>" . $record['Image'] . "</td>";
    echo "</tr>";
}
echo "</table>";

mysql_close($con);

?>

</body>
</html>

Thanks for any sort of feedback!

  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 15 '16 at 14:55
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Feb 15 '16 at 14:55
  • 2
    http://stackoverflow.com/questions/7793009/how-to-retrieve-images-from-mysql-database-and-display-in-an-html-tag – Shad Feb 15 '16 at 15:14
  • I just added comma please try once again , you can test the db record of image in static html as data-uri – Shad Feb 16 '16 at 09:04

2 Answers2

1

Try this

 echo "<td><img src='/{$record['Image']}'>" . $record['Image'] . "</td>";
paranoid
  • 6,799
  • 19
  • 49
  • 86
  • I haven't assigned any directory for storing my pictures, I've only sent them to the table Lectures in my db. How could i retrieve it from there? – Steffan Larsen Feb 15 '16 at 15:07
  • This is my result when I tried that. As you can see I've only added an image on one of the three inserts, but I'm still only able to display the image-name. [link](http://bildr.no/image/alFMeUsy.jpeg) – Steffan Larsen Feb 15 '16 at 15:15
  • 1
    This will not work if you are fetching images from DB, this can work only when you fetch image path from DB and images stored in directory. – Shad Feb 16 '16 at 04:42
0

I took this answer from linked answer that I had put in comments. There are other alternatives also provided for this problem.

    echo "<td>" . $record['Image'] . "</td>";
     replace with 
    echo "<td><img src=\"data:image/jpeg;base64,"" . base64_encode( $record['Image'] ) . "/></td>";

You are storing images in DB and while fetching it is giving you image data that is raw so to display images from data you can use this method. This wont help in caching but it will solve the problem.

Shad
  • 969
  • 1
  • 10
  • 18
  • By adding the given line I get the following syntax error: unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) – Steffan Larsen Feb 16 '16 at 08:34
  • Now I get the following outcome: [link](http://bildr.no/image/YU84Skgr.jpeg) As you can see it now tries to add the same image on all of the inserts, which was not what I intended to do. – Steffan Larsen Feb 16 '16 at 08:48
  • Please check once again. You can test it in static html with your image record data like this Red dot – Shad Feb 16 '16 at 09:06
  • In your link I can't see any image. could you please send it again – Shad Feb 16 '16 at 09:21
  • can you please check into chrome developer tool? if there images are loading properly or not and just copy the html of one image html tag and past so that I can see , why it is adding same image on all – Shad Feb 16 '16 at 09:46