简体   繁体   English

如果 mysql 中没有记录,php 显示默认图像

[英]php display default images if no record in mysql

I got below php code to search and display images from mysql, it's working fine so far, i want to add a feature to display a default images if the search result got no record found in mysql ($field5name).我得到了下面的 php 代码来搜索和显示来自 mysql 的图像,到目前为止它工作正常,如果搜索结果在 Z81C3B080DAD537DE7E10E0987A 中找不到记录,我想添加一个显示默认图像的功能(4)。

I have tried to add an 'onerror' function inside but not working, what is missing from my code?我试图在里面添加一个'onerror' function 但不起作用,我的代码中缺少什么?

<!DOCTYPE html>
<html>  
  <body>
    <!-- [SEARCH FORM] -->
    <form method="post" action="1-form.php">
      <h1>SEARCH FOR USERS</h1>
      <input type="text" name="search" required/>
      <input type="submit" value="Search"/>
    </form>
<br/>
<div align="left"><Font size="4"><a href="logout.php" style="margin-right:80px;">LOGOUT</a></font></div><br>
    <?php
if (isset($_POST['search'])) {
    require "2-search.php";
    echo '<table border="1" cellspacing="2" cellpadding="2"> 
      <tr> 
          <td> <font face="Arial">ID</font> </td> 
          <td> <font face="Arial">OTP</font> </td> 
          <td> <font face="Arial">EMAIL</font> </td> 
          <td> <font face="Arial">STATUS</font> </td> 
          <td> <font face="Arial">TIMESTAMP</font> </td> 
      </tr>';
    // DISPLAY RESULTS
    if (count($results) > 0) {
        foreach ($results as $r) {
            $field1name = $r["id"];
            $field2name = $r["otp"];
            $field3name = $r["email"];
            $field4name = $r["is_expired"];
            // $field5name = $r["create_at"]; 
            $field5name = $r["jpeg_info"];
            echo '<tr> 
                  <td>' . $field1name . '</td> 
                  <td>' . $field2name . '</td> 
                  <td>' . $field3name . '</td> 
                  <td>' . $field4name . '</td> 
                  <td><a href = ' . $field5name . ' target = "_blank">
                        <img src=' . $field5name . '  height="100" width="100">
                  </td> //<------------------- i am stuck here
                   </tr>';
        }
        $result->free();
    } else {
        echo '<FONT COLOR="RED" SIZE="5"> NO RESULTS FOUND </FONT>';
    }
}
?>
 </body>
</html>

All you need to do is add a little test in the code to see if the field in question is empty.您需要做的就是在代码中添加一个小测试,看看有问题的字段是否为空。 Not sure what empty is to you so I assumed ''不确定对你来说什么是空的,所以我假设''

if (count($results) > 0) {
    foreach ($results as $r) {
        $field1name = $r["id"];
        $field2name = $r["otp"];
        $field3name = $r["email"];
        $field4name = $r["is_expired"];
        // $field5name = $r["create_at"]; 
        $field5name = $r["jpeg_info"];

        if ( $field5name == '' ) {
            $img = 'default.png';
        } else {
            $img = $field5name;
        }

        echo "<tr> 
              <td>$field1name</td> 
              <td>$field2name</td> 
              <td>$field3name</td> 
              <td>$field4name</td>";
              <td>
                <a href='$img' target='_blank'>
                    <img src='$img' height='100' width='100'>
                </a>
               </td>
            </tr>";
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM