Json
Json

Reputation: 23

How to display images from phpmysql server with base64_encode? [PHP + HTML]

This image shows the products table that I am using to upload my image from the back-end side and I wanted to display it on the front-end page. I realize that the picture that I have uploaded through the back end side only saved as '48 B' but when I was uploading it through MySQL manually it saved as '64 KiB', although both are the same file.

When it comes to displaying the 48 B Image file would not show.

[code1] This is the code that I am using to insert the image from the back-end.

if(isset($_POST['submit'])){
$pid = mysql_real_escape_string($_POST['productID']);
$productName = mysql_real_escape_string($_POST['productName']);
$productImg = mysql_real_escape_string($_POST['productImg']);

$insert = mysql_query("INSERT INTO products (productID, productName, productImg) VALUES ('$pid', '$productName', '$productImg')");

if($insert)
{
    echo 'Successfully added new record.';
} else {
        echo 'Failed to add new record because '.mysql_error();
       }

} ?>

[code2] and this is the code that I am using to display the image from the front-end.

while($row = mysql_fetch_array($query)){
    echo '<tr>  

        <td>'.$row['productID'].'</td>
        <td>'.$row['productName'].'</td>
        <td>'. 
                '<img src="data:image/jpg;base64,'.base64_encode($row['productImg']).'" width="auto" height="150" class="img-thumnail"/>'
        .'</td>

I think the problem is in the insertion code [code1] even though all the data successfully inserted.

Upvotes: 1

Views: 44

Answers (1)

saketh
saketh

Reputation: 327

if you are using <input type="file" /> and if you are not aware this that "you cannot send file type like normal string" check that what you are doing before submit.

Refer this link it may help you:
https://www.codexworld.com/store-retrieve-image-from-database-mysql-php/

Upvotes: 1

Related Questions