如何从我的数据库在PHP页面上显示图像?

时间:2019-05-13 12:35:09

标签: php database image phpmyadmin

我正在尝试根据用户以表格(IMG 1)输入的内容显示图像和相关数据。一切正常,但是我需要表格显示实际图像而不是URL。

在我的数据库中,我已经将图像数据存储为URL,但是我想在PHP页面(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

所以在表格(IMG 2)中,我希望显示实际图像而不是链接

这是执行搜索并显示数据的代码中最重要的部分。

    $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 ";
    }

如有需要,我可以提供其他信息。

如何解决此问题?

2 个答案:

答案 0 :(得分:1)

只需编辑一下这段代码即可:

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 ";
    }

它所做的就是使用HTML的<img />标签将URL显示为图像。另外,alt为图像添加标题,如果您将鼠标悬停在图像上,则可以看到该属性的值。

答案 1 :(得分:-1)

首先:您的代码非常不安全。它容易受到sql注入和XSS的攻击。<​​/ p>

关于您的问题:您需要输出一个以图片网址作为src属性的html标签。

赞:

echo "<td><img src='" . htmlspecialchars($row["Reference-to-S3"]) . "' /></td>\n";