php - 搜索前如何隐藏所有sql表?

标签 php mysql sql

在我的代码中搜索之前,它会显示所有用户数据。我想要的是,只有在按下搜索选项后,它才会显示特定的用户数据,但在搜索之前,我想隐藏所有数据。下图是我的界面

interface of search

急切寻求帮助并提前致谢。

这是我的代码

<?php 
$db = new PDO('mysql:dbname=mypro_bms;host=localhost', 'root', '');
if (isset($_GET['q'])) {
  $q = $_GET['q'];
  $statement = $db->prepare("select * from donate where passport_IC like :passport_IC");
  $statement->execute([
    ':passport_IC' => '%' . $q .'%'

  ]);
} else {
  $statement = $db->prepare('select * from donate');
  $statement->execute();
}
$people = $statement->fetchAll(PDO::FETCH_OBJ);
 ?> 
  <table class="table table-bordered">
          <tr>
            <th><center> id</center></th>
            <th><center> Passport/IC</center></th>
            <th><center> Blood Group</center></th>
            <th><center> Blood Bag Type</center></th>
            <th><center>Donation Date</center></th>
            <th><center>Action</center></th>




          </tr>
          <?php foreach($people as $donors): ?>
            <tr>
               <td><center><b><font color="black"><?= $donors->id; ?></font></b></center></td>
               <td><center><b><font color="black"><?= $donors->passport_ic; ?></font></b></center></td>
               <td><center><b><font color="black"><?= $donors->blood_group; ?></font></b></center></td>
               <td><center><b><font color="black"><?= $donors->blood_bag; ?></font></b></center></td>
               <td><center><b><font color="black"><?= $donors->donate_date; ?></font></b></center></td>


            </tr>
          <?php endforeach; ?>

        </table>

最佳答案

您必须将此部分保存在单独的文件中,并从 ajax 调用中调用它。

将代码保留在文件search.php中

<table class="table table-bordered">
      <tr>
        <th><center> id</center></th>
        <th><center> Passport/IC</center></th>
        <th><center> Blood Group</center></th>
        <th><center> Blood Bag Type</center></th>
        <th><center>Donation Date</center></th>
        <th><center>Action</center></th>    
      </tr>
      <?php foreach($people as $donors): ?>
        <tr>
           <td><center><b><font color="black"><?= $donors->id; ?></font></b></center></td>
           <td><center><b><font color="black"><?= $donors->passport_ic; ?></font></b></center></td>
           <td><center><b><font color="black"><?= $donors->blood_group; ?></font></b></center></td>
           <td><center><b><font color="black"><?= $donors->blood_bag; ?></font></b></center></td>
           <td><center><b><font color="black"><?= $donors->donate_date; ?></font></b></center></td>    
        </tr>
      <?php endforeach; ?>    
    </table>

将代码保留在文件 fetchsearchresult.php

<?php 
$db = new PDO('mysql:dbname=mypro_bms;host=localhost', 'root', '');
if (isset($_GET['q'])) {
  $q = $_GET['q'];
  $statement = $db->prepare("select * from donate where passport_IC like :passport_IC");
  $statement->execute([
    ':passport_IC' => '%' . $q .'%'    
  ]);
} else {
  $statement = $db->prepare('select * from donate');
  $statement->execute();
}
$people = $statement->fetchAll(PDO::FETCH_OBJ);
// Here you have to pass the data($people) to search.php file
 ?> 

现在,从 search.php 文件中,通过单击搜索按钮,您必须通过 ajax 调用来调用 fetchsearchresult.php 来获取搜索结果。获得结果后,您已将该数据绑定(bind)到 search.php

注意:如果 $_GET['q'] 变量没有获取值,它将从表中获取所有详细信息。

希望对你有帮助

关于php - 搜索前如何隐藏所有sql表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55508383/

相关文章:

php - datatables.net 列对另一列进行排序

php - MySQL加入: unknown column in field list

php - 通知: Undefined variable username when I try to echo $username

MySQL查询问题

php - mysql_real_query 与 mysql_query

php - Hooks around 添​​加到 Woocommerce 的购物车

sql - SQL追加的Delphi 7中的表达式类型不匹配

SQL服务器错误: The INSERT statement conflicted with the CHECK constraint

php - 使用 Thrift 进行 PHP 到 C++ 的翻译

php echo 无法打印