0

Good day!

I have created a form to insert and retrieve data. I need to add a section to insert an image through this form into the database and display it back. But, I don't know the right way to do it because I've tried various ways and still no results :(

I managed to insert the image into the database. But, I failed to retrieve it and view it on my view.php. Here is the code I have made.

view.php

<div class="modal-dialog">
        <div class="modal-content">
            <form method="POST" action="view.php">  
                <div class="modal-header">
                    <form action="papar.php" method="post">
                </div>
                <div class="modal-body">
                    <div class="col-md-2"></div>
                    <div class="col-md-8">
                        <div class="form-group">
                            <label><strong>ID</strong></label><br>
                            <?php echo $id ?>
                        </div>
                        <div class="form-group">
                            <label><strong>NAME</strong></label><br>
                            <?php echo $name ?>
                        </div>
                        <div class="form-group">
                            <label><strong>LOCATION</strong></label><br>
                            <?php echo $location ?>
                        </div>
                        <div class="form-group">
                            <label><strong>CONTACT NUMBER</strong></label><br>
                            <?php echo $contact_details ?>
                        </div>
                        <div class="form-group">
                            <label><strong>BRANCH</strong></label><br>
                            <?php echo $branch ?>
                            
                        </div>
                        <div class="form-group">
                            <label><strong>STATUS</strong></label><br>
                            <?php echo $status ?>
                            
                        </div>
                        <div class="form-group">
                            <label><strong>CATEGORY</strong></label><br>
                            <?php echo $category ?>
                            
                        </div>
                        <div class="form-group">
                            <label><strong>MAP (PETA)</strong></label><br>
                             <?php  
            $query = "SELECT * FROM `customers.contact` ORDER BY id ";  
            $result = mysqli_query($link, $query);  
            while($row = mysqli_fetch_array($result))  
            {  
                 echo '  
             <img src="data:image/jpeg;base64,'.base64_encode($row['map'] ).'" height="200" width="200" 
                 ';  
            }  
            ?>  
                        </div>

                        <div class="form-group">
                            <label><strong>DETAILS</strong></label><br>
                                <?php echo $details?>        
                        </div>
                        
                        <div class="form-group">
                            <label><strong>FOLLOW UP DATE</strong></label><br>
                            <?php echo $followup_date?>
                        </div>
                    </div>
                </div>
                
                <div style="clear:both;"></div>
                <div class="modal-footer">
                    <input type="button" value="Back" onclick="history.back(-1)" />
                </div>
            </form> 
        </div>
    </div>


            </div>

This is the result according to view.php

  • Can't tell about adding images as raw data to a DB, but it is simpler if you write an upload script, save the image to an image folder and save the image name in your DB, when you need it, you just need to reference the image name from the DB and the image folder in your server – AugustoM Mar 17 '21 at 05:11

1 Answers1

0

you didn't close the image tag

echo '<img src="data:image/jpeg;base64,'.base64_encode($row['map'] ).'" height="200" width="200" ';

instead of

echo '<img src="data:image/jpeg;base64,'.base64_encode($row['map'] ).'" height="200" width="200" />';

but its best to upload the image to a folder and then store the image name in a folder then call it like so -

echo '<img src="your-image-folder-name/'.$row['map'].' " />'

for uploading files to a folder with php

check out this link: https://www.w3schools.com/php/php_file_upload.asp

Anthony phillips
  • 152
  • 3
  • 13