Home » Php » Image is not displaying using PHP PDO and MYSQL

Image is not displaying using PHP PDO and MYSQL

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am working on PHP PDO. I want to insert and display image in MYSQL database.

My table data –

itemImage longblob ; 

I tried this PHP PDO code –

 $itemImage = addslashes(file_get_contents($_FILES["image"]["tmp_name"])); 

    $stmt = $db->prepare('INSERT INTO items (itemImage) VALUES (:itemImage)') ;
$stmt->execute(array(

    ':itemImage' => $itemImage

));

HTML form –

 <form action='' method='post' enctype='multipart/form-data'>
        <p><label>Item Image </label><br />
<input class="input-group" type="file" name="image" value='<?php if(isset($error)){ echo $_POST['image'];}?>' id="image" style="width:100%;" ></p>


        <p><input type='submit' name='submit' value='Submit'></p>

    </form>

It inserts the blob image .

I am displaying image using the code below .

<img src="data:image/jpeg;base64,'.base64_encode($itemImage ).'" height="300" width="300" style="width:100%" class="img-thumnail" />  

If I upload the image using my HTML form, the image does not display.

if I upload the image using MySQL PHPMyAdmin(Insert), image displays proper

What is the reason behind this?

How to&Answers: