如何绕过<img src...="" causing="" broken="" image="" icon?

时间:2017-12-18 19:13:04

标签: php image

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

我的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>";

getImage.php文件:

<?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'];

知道如何查看图片吗? 下面的代码是图像的存储方式。

<?php
if ($image_size == FALSE){
//echo "image_size = false";
header('Location: index.php');
} else {
$query = "INSERT INTO PostImages (name, image, ImageDATETIME) 
    VALUES (:image_name, :image, :DATETIME)";
$statement = $db->prepare($query);
$statement->bindvalue(':image_name', $image_name);
$statement->bindvalue(':image', $image);
$statement->bindvalue(':DATETIME', $DATETIME);
$statement->execute();
$statement->closeCursor();
}
?>

enter image description here

2 个答案:

答案 0 :(得分:2)

这是一个解决方案:

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

说明:由于您要使用PostImages表加入forumPosts,因此需要检查是否存在只能来自PostImages列的列数据。所以我添加了if语句来检查ID是否存在。假设这来自PostImages表。

附录:将if语句放在表格中间没有任何问题。这就是动态生成HTML结构的方式。唯一的考虑因素是您不会生成无效的HTML,例如省略结束标记(如</td>)。但是,如果图像不存在,则if语句只会省略<img>标记,并且不会使表格的HTML无效。

答案 1 :(得分:0)

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

if (!empty($row['image']) {
    echo $row['image'];
} else {
    echo 'empty';
}

在其他内容中,您可以找出在空$row['image']变量的情况下要执行或显示的内容。

相关问题