Php 我怎样才能四处走动<;img src。。。导致图像图标损坏?

Php 我怎样才能四处走动<;img src。。。导致图像图标损坏?,php,image,Php,Image,在PHP中,我可以将图像上传到数据库并显示在表中。但是,如果没有图像,则在帖子中会显示一个损坏的图像图标。我想知道如何在显示之前检查图像,这样我就不会得到损坏的图像图标。我最近确定损坏的图像图标是由index.php文件中的图像标记引起的。我在注释getImage.php文件中的代码时发现了这一点。我不相信我能在我中间有一个IF的声明,回响着一张桌子。我们将不胜感激 my index.php文件中的代码: include('connect.php'); $query = 'SELECT * FR

在PHP中,我可以将图像上传到数据库并显示在表中。但是,如果没有图像,则在帖子中会显示一个损坏的图像图标。我想知道如何在显示之前检查图像,这样我就不会得到损坏的图像图标。我最近确定损坏的图像图标是由index.php文件中的图像标记引起的。我在注释getImage.php文件中的代码时发现了这一点。我不相信我能在我中间有一个IF的声明,回响着一张桌子。我们将不胜感激

my index.php文件中的代码:

include('connect.php');
$query = 'SELECT * FROM forumPosts LEFT JOIN PostImages ON 
forumPosts.DATETIME = PostImages.ImageDATETIME ORDER BY replyIndex 
ASC';
$statement = $db->prepare($query);
$statement->execute();
$posts = $statement->fetchAll();
$statement->closeCursor();
echo "<table>";
foreach ($posts as $post){
if ($post['post_type'] == "r"){
    $post_id = $post['post_id'];
        echo "<tr bgcolor='beige'><td>reply</td><td>". 
        $post['post_title'] . "</td ><td>". $post['post_body'] . "
        <img src='getImage.php?id=".$post['ID']."'>". "</td><td>". 
        $post['DATETIME']. "</td><td>". $post['replyIndex']. "</td>
        <td>".$post['post_type']."</td>";
<?php
include('connect.php');

$ID = $_GET['id'];

$query = "SELECT * FROM PostImages WHERE ID=:ID";
$statement = $db->prepare($query);
$statement->bindvalue(':ID', $ID);
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC);

header("Content-type: image/jpeg");
echo $row['image'];
include('connect.php');
$query='SELECT*FROM forumPosts LEFT JOIN POSTIGES ON
forumPosts.DATETIME=PostImages.ImageDATETIME按replyIndex排序
ASC′;
$statement=$db->prepare($query);
$statement->execute();
$posts=$statement->fetchAll();
$statement->closeCursor();
回声“;
foreach($posts作为$post){
如果($post['post_type']==“r”){
$post_id=$post['post_id'];
回应“答复”。
$post['post\u title'..”.$post['post\u body'.]
". "". 
$post['DATETIME']。“.$post['replyIndex']”
“$post['post_type']”;
以及getImage.php文件:

include('connect.php');
$query = 'SELECT * FROM forumPosts LEFT JOIN PostImages ON 
forumPosts.DATETIME = PostImages.ImageDATETIME ORDER BY replyIndex 
ASC';
$statement = $db->prepare($query);
$statement->execute();
$posts = $statement->fetchAll();
$statement->closeCursor();
echo "<table>";
foreach ($posts as $post){
if ($post['post_type'] == "r"){
    $post_id = $post['post_id'];
        echo "<tr bgcolor='beige'><td>reply</td><td>". 
        $post['post_title'] . "</td ><td>". $post['post_body'] . "
        <img src='getImage.php?id=".$post['ID']."'>". "</td><td>". 
        $post['DATETIME']. "</td><td>". $post['replyIndex']. "</td>
        <td>".$post['post_type']."</td>";
<?php
include('connect.php');

$ID = $_GET['id'];

$query = "SELECT * FROM PostImages WHERE ID=:ID";
$statement = $db->prepare($query);
$statement->bindvalue(':ID', $ID);
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC);

header("Content-type: image/jpeg");
echo $row['image'];


要扩展FirstOne的答案,您可以使用基本逻辑检查:

if (!empty($row['image']) {
    echo $row['image'];
} else {
    echo 'empty';
}
在else中,您可以了解在出现空
$row['image']
变量的情况下要做什么或显示什么。

以下是一个解决方案:

include('connect.php');
$query = 'SELECT * FROM forumPosts LEFT JOIN PostImages ON 
forumPosts.DATETIME = PostImages.ImageDATETIME ORDER BY replyIndex 
ASC';
$statement = $db->prepare($query);
$statement->execute();
$posts = $statement->fetchAll();
$statement->closeCursor();
echo "<table>";
foreach ($posts as $post){
if ($post['post_type'] == "r"){
    $post_id = $post['post_id'];
        echo "<tr bgcolor='beige'><td>reply</td><td>". 
        $post['post_title'] . "</td >";
        echo "<td>". $post['post_body'];
        if (!empty($post['ID'])) {
            echo "<img src='getImage.php?id=".$post['ID']."'>";
        }
        echo "</td><td>". 
        $post['DATETIME']. "</td><td>". $post['replyIndex']. "</td>
        <td>".$post['post_type']."</td>";
include('connect.php');
$query='SELECT*FROM forumPosts LEFT JOIN POSTIGES ON
forumPosts.DATETIME=PostImages.ImageDATETIME按replyIndex排序
ASC′;
$statement=$db->prepare($query);
$statement->execute();
$posts=$statement->fetchAll();
$statement->closeCursor();
回声“;
foreach($posts作为$post){
如果($post['post_type']==“r”){
$post_id=$post['post_id'];
回应“答复”。
$post['post_title'];
回声“.$post['post_body'”;
如果(!empty($post['ID'])){
回声“;
}
“回声”。
$post['DATETIME']。“.$post['replyIndex']”
“$post['post_type']”;
说明:由于要将forumPosts与PostImages表连接,因此需要检查是否存在只能来自PostImages列的列数据。因此,我添加了一个
if
语句,用于检查
ID
是否存在。假设这是来自PostImages表


附录:在表的中间放一个<代码>如果语句是没有错的。这就是你如何动态生成HTML结构。唯一的考虑应该是你不生成无效的HTML,比如省去一个结束标签(如<代码> <代码>)。。但是如果图像不存在,则此

if
语句只会省略
标记,这不会使表的HTML无效。

等等,你是否要求检查图像是否在驱动器中?我想检查数据库,看看是否有图像,这样我就不会得到损坏的图像图标。@MattMacy-你真的存储了吗你的文件系统中某个地方的图像?上面的查询似乎只在数据库中存储值。你能给我们一个带有数据的表的屏幕截图吗?你能展示一下你是如何生成
$posts
数组的吗?如果你正在进行连接(我假设你要连接posts和post图像),然后您应该添加一个
if
语句,检查是否存在只能来自PostImages表的列。重读后,我认为OP存储图像的方式不是这样的。请检查
src
。它们将post ID传递给将加载图像的页面。这样,链接将已经创建,页面创建It不知道图像是否存在。哦,我现在明白了。有趣的架构。如果他在执行查询,然后返回带有选中表列的$row,我们应该安全地假设$row['image']将为空,对吗(除非我进一步误解)。添加了这些代码后,我就看不到图像了。这应该很简单。@MattMacy,你和我!在理解图像的结构以及图像的存储方式(位置、方式、格式)时遇到了一些困难。图像块存储在本地主机上的数据库中