简体   繁体   English

在搜索之前如何隐藏所有sql表?

[英]Before search how to hide all sql table?

In my code before searching its showing all user data. 在我的代码中搜索它显示所有用户数据之前。 What I want is that only after pressing search option, will it show specific user data but before the search, I want to hide all the data. 我想要的是,只有在按下搜索选项后,它才会显示特定的用户数据,但在搜索之前,我想要隐藏所有数据。 The picture below is my interface 下图是我的界面

搜索界面

Badly looking for assistance and thanks in advance. 非常寻求帮助,并提前感谢。

Here is my code 这是我的代码

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

You have to keep this part in seprate file and call this from ajax call. 您必须将此部分保留在seprate文件中并从ajax调用中调用它。

Keep codes in File search.php 将代码保存在文件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>

Keep codes in File fetchsearchresult.php 将代码保存在File 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
 ?> 

Now from search.php file, by clicking search button you have to call fetchsearchresult.php through ajax call to get the search result. 现在从search.php文件中,通过单击搜索按钮,您必须通过ajax调用调用fetchsearchresult.php来获取搜索结果。 After getting result you have bind that data in to search.php 获得结果后,您已将该数据绑定到search.php

Note: if $_GET['q'] variable didn't get value it'll fetch all detail from table. 注意:如果$ _GET ['q']变量没有得到值,它将从表中获取所有细节。

Hope it helps 希望能帮助到你

Since you don't want to show any data before the user makes a search, you need to remove the else-block: 由于您不希望在用户进行搜索之前显示任何数据,因此您需要删除else-block:

<?php
$db = new PDO('mysql:dbname=mypro_bms;host=localhost', 'root', '');

// Default value for the $people array
$people = [];

// We're using empty() to make sure that the search string actually contains something.
if (!empty($_GET['q'])) {
    $statement = $db->prepare("select * from donate where passport_IC like :passport_IC");
    $statement->execute([
        ':passport_IC' => '%' . $_GET['q'] .'%'
    ]);

    // We moved this inside of the if-statements
    $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>

This shouldn't give you any errors, since we've created a default value for $people and moved the fetchAll() to go inside of the if-statement. 这不应该给你任何错误,因为我们已经为$people创建了一个默认值,并将fetchAll()移到if语句中。

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

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