【问题标题】:HTML / PHP / SQL How to display a button under certain circumstances?HTML / PHP / SQL 在某些情况下如何显示按钮?
【发布时间】:2019-02-12 05:00:04
【问题描述】:

所以我有一个使用 HTML、PHP 和 mysql 的脚本,我想在某些情况下显示一个按钮。 这是我的脚本:

<?php
include_once('dbconnect.php');
    $q = $_POST['q'];
    $q = $_GET['query']; 
    $query = mysqli_query($conn,"SELECT * FROM `Persons` WHERE `id` LIKE '%$q%'"); 
    $count = mysqli_num_rows($query);
    if($count != "1"){
        $output = '<h2>No result found!</h2>';
    }else{
        while($row = mysqli_fetch_array($query)){
        $s = $row['name'];
                $output .= '<h2>Found: '.$s.'</h2><br>';
            }
        }
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Search</title>
    </head>
    <body>
        <form method="POST" action="index.html">
            <input type="submit" name="return" value="Return">
        </form>
        <?php echo $output; ?>
    </body>
</html>

具体来说,我只想在输出为“未找到结果”时显示返回按钮,当 SQL 数据库中与给定查询匹配的行数不是 1 时。我该怎么做呢?我对 PHP 和 mySQLi 比较陌生,但是从我的研究中我无法弄清楚如何完成这样的任务,有什么想法吗?

【问题讨论】:

  • 通过使用简单的 php if 语句,您可以在上面代码的 html 位中的 &lt;?php ?&gt; 中执行此操作。

标签: php html mysqli


【解决方案1】:
<?php 
if ($count==0) {
 echo '<input type="submit" name="return" value="Return">';
}
?>

【讨论】:

    【解决方案2】:

    如果您想要更简洁的 html 代码,请执行以下操作:

    <form method="POST" action="index.html">
        <?php if ($count!= "1") : ?>
        <input type="submit" name="return" value="Return">
        <?php else : ?>
        <!-- put your other button here -->
        <?php endif; ?>
    </form>
    

    您可以阅读有关从 HTML 转义 here 的更多信息。

    【讨论】:

      【解决方案3】:
      <?php
          include_once('dbconnect.php');
          $q = $_POST['q'];
          $q = $_GET['query']; 
          $query = mysqli_query($conn,"SELECT * FROM `Persons` WHERE `id` LIKE '%$q%'"); 
          $results = mysqli_fetch_array($query);
      ?>
      <!DOCTYPE html>
      <html>
          <head>
              <title>Search</title>
          </head>
          <body>
              <form method="POST" action="index.html">
                  <input type="submit" name="return" value="Return">
              </form>
              <?php if(0 < count($results)) ?>
              <?php foreach($results AS $row) : ?>
              <h2><?= $row['name'] ?></h2>
              <?php endforeach; ?>
              <?php else : ?>
              <H2> No results found!</h2>
              <?php endif; ?>
          </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-15
        • 1970-01-01
        相关资源
        最近更新 更多