0

I am trying to render uploaded image from Mysql to frontend in a file called result.php This is the code that uploaded the image to Mysql

include ('connect.php');
//if button with the name uploadfilesub has been clicked
if(isset($_POST['uploadfilesub'])) {
//declaring variables
$filename = $_FILES['uploadfile']['name'];
$filetmpname = $_FILES['uploadfile']['tmp_name'];
//folder where images will be uploaded
$folder = 'imagesuploadedf/';
//function for saving the uploaded images in a specific folder
move_uploaded_file($filetmpname, $folder.$filename);
//inserting image details (ie image name) in the database
$sql = "INSERT INTO `uploadedimage` (`imagename`) VALUES ('$filename')";
$qry = mysqli_query($conn, $sql);
if( $qry) {
echo "</br>image uploaded";
}
}
?>

The uploaded images are saved on a folder on my server called imagesuploadedf

I used this code below to render the image on the result.php but it's showing the file name only, I need the image to be rendered.

$conn = new mysqli($servername, $username, $password, $dbname);

$sql = 'SELECT * from uploadedimage';

if (mysqli_query($conn, $sql)) {

echo "";

} else {

echo "Error: " . $sql . "<br>" . mysqli_error($conn);

}

$count=1;

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {

// output data of each row

while($row = mysqli_fetch_assoc($result)) { ?>

<tbody>

<tr>

<td>

<?php echo $row['imagename']; ?>

</td>

</tr>

</tbody>

<?php

$count++;

}

} else {

echo '0 results';

}

?>

</table>

What do I need to write on result.php to show/render the uploaded image on the page?

O. Jones
  • 103,626
  • 17
  • 118
  • 172
GreenCode
  • 3
  • 1
  • (Possible) side note: Do not use string interpolation or concatenation to get values into SQL queries. That's error prone and might make your program vulnerable to SQL injection attacks. Use parameterized queries. See ["How to include a PHP variable inside a MySQL statement"](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement) and ["How can I prevent SQL injection in PHP?"](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – sticky bit Mar 06 '21 at 17:14
  • Thanks @stickybit , but this comment doesn't solve my problem – GreenCode Mar 06 '21 at 17:27

1 Answers1

0

You should write an HTML page containing <img> elements referring to the images you want to display.

It might look like this:

<td>
<?php 
$imgTag = '<img src="/imagesuploadedf/' . $row['imagename'] . '" />';
echo $imgTag; ?>
</td>

This puts out stuff like this into your web page.

<td>
  <img src="/imagesuploadedf/image_file_name"/>
</td>

If your web page is coming from https://images.example.com this will make the browser download the image at https://images.example.com/imagesuploadedf/image_file_name from your server and display it in the page.

O. Jones
  • 103,626
  • 17
  • 118
  • 172