skip to Main Content

I’m trying to display images and relevant data based on what the user enters in a form(IMG 1). Everything is working but I need the table to show the actual image instead of URL.

In my database, I’ve got the image data stored as URL but I want to show as an image on the PHP page (IMG 3).

IMG – 1: What User Enters

enter image description here

IMG – 2: Result of Search
enter image description here

IMG – 3: Set up on phpMyAdmin
enter image description here

So in the table (IMG 2), I want the actual image to show up instead of link

Here is the most important part of the code that does the searching and displays the data.

    $title = trim($_POST["PhotoTitle"]);
    $FromDate = trim($_POST["FromDate"]);
    $ToDate = trim($_POST["ToDate"]);
    $keywords = trim($_POST["keywords"]);

    $query ="SELECT * FROM `photos` WHERE `Photo-Title` like '%".$title."%' AND `Keywords` like '%".$keywords."%' AND `Date-of-Photo` BETWEEN '$FromDate' AND '$ToDate'";

    while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr>n ";
        echo "<td>",$row["Photo-Title"],"</td>n";
        echo "<td>",$row["Description"],"</td>n";
        echo "<td>",$row["Date-of-Photo"],"</td>n";
        echo "<td>",$row["Keywords"],"</td>n";
        echo "<td>",$row["Reference-to-S3"],"</td>n";
        echo "</tr>n ";
    }

I can provide any other information if needed.

How do I solve this problem?

2

Answers


  1. First: Your code is highly insecure. It is vulnerable to sql injections and XSS.

    Regarding your Question: You need to output an html tag with the image url as the src attribute.

    Like this:

    echo "<td><img src='" . htmlspecialchars($row["Reference-to-S3"]) . "' /></td>n";
    
    Login or Signup to reply.
  2. Just edit your while to this piece of code :

    while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr>n ";
            echo "<td>",$row["Photo-Title"],"</td>n";
            echo "<td>",$row["Description"],"</td>n";
            echo "<td>",$row["Date-of-Photo"],"</td>n";
            echo "<td>",$row["Keywords"],"</td>n";
            echo "<td><img src='" . $row["Reference-to-S3"] . "' alt='" . $row["Photo-Title"] . "' /></td>n";
            echo "</tr>n ";
        }
    

    what it does is displaying a URL as an image using <img /> tag of HTML. Also, alt adds a title to the image which if you hover image, you can see the value of this attribute.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search