在我的代码中搜索之前,它会显示所有用户数据。我想要的是,只有在按下搜索选项后,它才会显示特定的用户数据,但在搜索之前,我想隐藏所有数据。下图是我的界面
急切寻求帮助并提前致谢。
这是我的代码
<?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/