为什么要显示来自 [=11g=] 数据库的所有数据,如果我通过将搜索字段保持空白来点击搜索按钮,并且所有条目都需要额外的 HTML 按钮。

Why show all data from mysql databese if I hit saarch button by keep search field blank and need extra one HTML button for all Entries.?

当我将搜索字段留空并点击搜索按钮时,然后显示来自 mysql 数据库的所有结果,为什么...这是我的 php 代码....

我想创建它,当我将搜索字段留空并单击搜索按钮时...必须显示错误 "no search result" 并且想要创建不允许的空格搜索并需要额外的一个 HTML所有条目的按钮。一键获取所有条目......

请帮帮我....

 <?php

$con=mysql_connect('localhost', '1093913', 'tanim1996');
$db=mysql_select_db('1093913');



if(isset($_POST['button'])){    //trigger button click

  $search=$_POST['search'];

  $query=mysql_query("select * from iconic19 where student_id like '%{$search}%' || name like '%{$search}%' || phone like '%{$search}%' || blood like '%{$search}%' || district like '%{$search}%' ");



if (mysql_num_rows($query) > 0) {                 
  while ($row = mysql_fetch_array($query)) {

 echo "<tbody>";
               echo "<tr>";
                echo "<td data-label='Student ID'>".$row['student_id']."</td>";
               echo "<td data-label='Name' style='font-weight:bold;' >".$row['name']."</td>";
                 echo "<td data-label='Mobile No'>"."<a href='tel:".$row['phone']."'>".$row['phone']."</a>"."</td>";
                echo "<td data-label='Blood' style='color:red; font-weight:bold;' >".$row['blood']."</td>";
                echo "<td data-label='Email'>"."<a href='mailto:".$row['email']."'>".$row['email']."</a>"."</td>";
                echo "<td data-label='District'>".$row['district']."</td>";
                echo "</tr>";
echo "</tbody>";

  }
}else{
    echo "<div class='error-text'>No results</div><br><br>";
  }

}else{                          //while not in use of search  returns all the values
  $query=mysql_query("select * from iconic19");

 while ($row = mysql_fetch_array($query)) {



  }


}

mysql_close();
?>

其Html代码

<form id="nbc-searchblue1" method="post" enctype="multipart/form-data" autocomplete="off">

    <input id='wc-searchblueinput1' placeholder="Search Iconic..." name="search" type="search" autofocus>
    <br>
    <input id='nbc-searchbluesubmit1' value="Search" type="submit" name="button">

   <div class="view-all"> <a href="script.php">Show all</a></div>

</form>

它的 css 代码..

.view-all a {
    background: red;
    padding: 10px;
    border-radius: 4px;
    color: #fff;
    text-decoration: none;
}

只需检查 $_POST['search'] 是否为空,然后显示您的消息,否则执行您的查询。

<?php

$con=mysql_connect('localhost', '1093913', 'tanim1996');
$db=mysql_select_db('1093913');

if(isset($_POST['button'])){    //trigger button click
    $numRows = 0;
    if(!empty($_POST['search'])) {
        $search = mysql_real_escape_string($_POST['search']);
        $query = mysql_query("select * from iconic19 where student_id like '%{$search}%' || name like '%{$search}%' || phone like '%{$search}%' || blood like '%{$search}%' || district like '%{$search}%' ");
        $numRows = (int)mysql_num_rows($query);
    }

    if ($numRows > 0) {                 
        while ($row = mysql_fetch_array($query)) {

            echo "<tbody>";
            echo "<tr>";
            echo "<td data-label='Student ID'>".$row['student_id']."</td>";
            echo "<td data-label='Name' style='font-weight:bold;' >".$row['name']."</td>";
            echo "<td data-label='Mobile No'>"."<a href='tel:".$row['phone']."'>".$row['phone']."</a>"."</td>";
            echo "<td data-label='Blood' style='color:red; font-weight:bold;' >".$row['blood']."</td>";
            echo "<td data-label='Email'>"."<a href='mailto:".$row['email']."'>".$row['email']."</a>"."</td>";
            echo "<td data-label='District'>".$row['district']."</td>";
            echo "</tr>";
            echo "</tbody>";

        }
    } else {
        echo "<div class='error-text'>No results</div><br><br>";
    }
} else {                          //while not in use of search  returns all the values
    $query = mysql_query("select * from iconic19");

    while ($row = mysql_fetch_array($query)) {



    }
}

mysql_close();

?>

我所做的是创建一个默认值为 0 的新变量 $numRows。如果您的搜索为空,则没有对数据库的查询。我逃脱了你的 $search 变量。

顺便说一句:请更改为 mysqli,较新的 php 版本不再支持 mysql 扩展。