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

时间:2019-04-04 05:19:37

标签: 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>

2 个答案:

答案 0 :(得分:0)

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

保留文件search.php中的代码

reference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                Data data = (Data) dataSnapshot.getValue(Data.class);
                dataList.add(data);
                //then notify data changed 
                recordAdapter.notifyDataSetChange();
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

               Data data = (Data) dataSnapshot.getValue(Data.class);
                //then find position of `data` in datalist to change some information of the item at position.
                //then notify data changed 
                recordAdapter.notifyDataSetChange(); 
            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
                Data data = (Data) dataSnapshot.getValue(Data.class);
                //then find data in datalist to remove, after removed, notify data set changed
                recordAdapter.notifyDataSetChange();
            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

保留文件fetchsearchresult.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>

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

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

希望有帮助

答案 1 :(得分:0)

由于您不想在用户进行搜索之前显示任何数据,因此需要删除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>

这应该不会给您带来任何错误,因为我们已经为$people创建了一个默认值,并将fetchAll()移到了if语句的内部。