HTML / PHP / SQL How to display a button under certain circumstances?

问题: So I have a script using HTML, PHP, and mysql, and I want to display a button under certain circumstances. Here is my script: <?php include_once('dbconnect.php');...

问题:

So I have a script using HTML, PHP, and mysql, and I want to display a button under certain circumstances. Here is my script:

<?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>

Specifically, I want to display the return button only when the output is "No results found", when the amount of rows in the SQL database matching the given query is not 1. How could I go about accomplishing this? I'm relatively new to PHP and mySQLi, but from my research I couldn't figure out how to do such a task, any ideas?


回答1:

If you want a much cleaner html code, do this:

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

You can read more about escaping from HTML here.


回答2:

<?php 
if ($count==0) {
 echo '<input type="submit" name="return" value="Return">';
}
?>

回答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>
  • 发表于 2019-02-14 18:25
  • 阅读 ( 183 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除