Nessy
Nessy

Reputation: 15

how to display image on form field with pdo php mysql

I'm trying to display an image on the HTML form. I have saved them in the DB as a BLOB. I understand this is not ideal but this is required for the small project I'm doing and there would be no more than 9 images at any given time but the user should be able to insert/update/delete them. I can echo the image in the browser with a while loop without any issues but when I try to do it in the form field, I only get a bunch of symbols. All data displays OK. Can you please point me in the right direction? I have tried to echo the img src, and anything else I was able to pickup from SO but it's not working. I only ask because I've run out of ideas! Many thanks in advance!

<?php   

  if(isset($_GET['id']) && !empty($_GET['id']))
         
   $id=$_GET['id'];   
    
   $conn = new PDO("mysql:host=localhost; dbname=e0912343_Fixtures", 'e0912343_Admin1','AdminPhp1');   
  
   $query=$conn->prepare("SELECT * FROM Teams WHERE TeamID=".$id);
             
   $query->execute(array(':TeamID'=>$id));    
          
?>

This is the while loop"

    <?php     
                    while ($row=$query->fetch())
                     {             
         
                     $id=$row["TeamID"];
              
                     $teamName=$row["TeamName"];
                     
                     $teamLogo=$row["TeamLogo"];
echo '<tr>            
            <td>'.$id.'</td>
            <td>'.$teamName.'</td>                   
         
            <td>'.'<img src = "data:image/png;base64,'.base64_encode($row['TeamLogo']).'" width = "50px" height = "50px"/>'
          . '</td>
               
            </tr>';  

This is the bit of the form/table that correspond to the image:

<td><img src = "data:image/png;base64,'.base64_encode($row['TeamLogo']).'" width = "50px" 
    height = "50px"/></td>
                                                  
   <td> <input type="file" name="TeamLogo" value= "<?php echo $teamLogo;?>"> </td>

   
              

Upvotes: 0

Views: 508

Answers (1)

Nessy
Nessy

Reputation: 15

just in case someone else is looking at this, I resolved it by embedding the while loop in the form and just assigning a tbl data tag to the image src. At least this worked for me:

<form method="post" enctype="multipart/form-data" action="Edit.php">
    
 
        <table class="table1">
        
        <?php     
            while ($row=$query->fetch())
              {             
     
              $id=$row["TeamID"];
          
              $teamName=$row["TeamName"];
                 
              $teamLogo=$row["TeamLogo"];          
          
            echo '<tr>         
           <td>'.'<label style="color:#3a87ad; font-size:18px;">Team Logo</label>'.'</td>                
         <td>'.'<img src = "data:image/png;base64,'.base64_encode($row['TeamLogo']).'" width = "50px" height = "50px"/>'
          . '</td>
               
            </tr>';         
    }
        ?>

    

Upvotes: 1

Related Questions