dongzhu3548
dongzhu3548
2016-02-06 11:08

在php中显示图像

已采纳

how to display blank if there is no image in a record.As i have inserted a record in database without an image but while fetching an record it is displaying an blank image in front end.It should not show any image if there is no image.Here is my code. If there is no image it should show only description.

Blogimage.php

<tbody>
<?php include "blogs.php" ;
while($row =  mysql_fetch_array($result))
{?>            
<tr>    
<td><img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/></td>
</tr>
<tr>
<td><?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?></td>
</tr>
<?php }?>
</tbody>

Blogs.php

$id=$_GET['title'];
$res = "SELECT *  FROM blogs 
 WHERE blog_title='$id'";
 $result=mysql_query($res);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duanba4942 duanba4942 5年前

    try changing this

    <img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/>
    

    to this

    <?php if($row['image']) echo "<img src='admin/upload/".$row['image']."' height='100' style='width:60%;height:50%;' />"; ?>
    
    点赞 评论 复制链接分享
  • dongxiong2000 dongxiong2000 5年前

    A couple of general points before we go into this

    1) The MySql library you are using is old and busted - you should be using something newer - read this - the options are Mysql PDO and MySqli - I promise you, you will be glad you did, i use IDIORM as an interface and find it very good (Also helps prevent stuff in point 2 below!)

    2) Your code can be SQL injected - more about that here - this is very bad :)

    Below is an example, I have fixed the SQL injection problem, and put in some logic that would test to see if there is an image, if not, it will dump out a 'noimage.jpg' src value.

    I have removed the include, and just simply put the code for blogs.php inline.

    <tbody>
    
        <?php 
    
        //START CODE FOR BLOGS.PHP
        $query = sprintf("SELECT *  FROM blogs WHERE blog_title='%s'",
            mysql_real_escape_string($_GET['title']));
        $result = mysql_query($query);
        //END CODE FOR BLOGS.PHP
    
        while($row =  mysql_fetch_array($result))
        {
    
            if(!empty($row['image']) && strlen($row['image']) > 4)
            {
                $iamgeSrc = 'admin/upload/' . $row['image'];
            }
            else
            {
                $imageSrc = 'admin/upload/noimage.jpg';
            }
        ?>            
            <tr>    
                <td>
                    <img src="<?php echo $imageSrc; ?>" height="100" style="width:60%;height:50%;"/>
                </td>
            </tr>
            <tr>
                <td>
                    <?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
                    </td>
            </tr>
        <?php 
        }
        ?>
    
    </tbody>
    

    If you want no image to show at all, then this would work

    <tbody>
    
        <?php 
    
        //START CODE FOR BLOGS.PHP
        $query = sprintf("SELECT *  FROM blogs WHERE blog_title='%s'",
            mysql_real_escape_string($_GET['title']));
        $result = mysql_query($query);
        //END CODE FOR BLOGS.PHP
    
        while($row =  mysql_fetch_array($result))
        {
            $image = '';
    
            if(!empty($row['image']) && strlen($row['image']) > 4)
            {
                $image = '<img src="admin/upload/'.$row['image'].'" height="100" style="width:60%;height:50%;"/>';
            }
    
        ?>            
            <tr>    
                <td>
                    <?php echo $image; ?>
                </td>
            </tr>
            <tr>
                <td>
                    <?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
                    </td>
            </tr>
        <?php 
        }
        ?>
    
    </tbody>
    
    点赞 评论 复制链接分享