[英]Php MysQl Search script returns blank page
我編寫了一個簡單的頁面腳本來循環遍歷從數據庫獲取數據的動態表。 但是,當我單擊搜索時,頁面將返回空白,沒有錯誤。 我試圖了解發生了什么而沒有成功。
display_table.php
<?php
include('session.php');
if ($_SESSION['login_user']){
include 'includes/header.php';
$searchQ = "SELECT * FROM companytable";
if(isset($_POST['search'])){
$search_term = mysqli_real_escape_string($db, $_POST['search_box']);
$searchQ .="WHERE title ='{$search_term}' ";
$searchQ .="OR country ='{$search_term}' ";
$searchQ .="OR description ='{$search_term}' ";
$searchQ .="OR timezone ='{$search_term}' ";
}
$query = mysqli_query($db, $searchQ) or die(mysqli_error());
}
形成
<form class="form" name="search_form" method="POST" action="display_table.php">
<input id="search_box" style="padding: 2px;" class="" type="text" name="search_box">
<input class="btn btn-default" type="submit" name="search" value="🔍">
</form>
表
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>Address</th>
<th>Country</th>
<th>Time Zone</th>
</tr>
<?php while($company=mysqli_fetch_array($result)){ ?>
<tr>
<td data-th="ID"><?=$company['id'];?></a></td>
<td data-th="Name"><?=$company['title'];?></td>
<td data-th="Description"><?=$company['description'];?></td>
<td data-th="Type"><?=$company['type'];?></td>
<td data-th="Address"><?=$company['address'];?></td>
<td data-th="Country"><?=$company['country'];?></td>
<td data-th="Time Zone"><?=$company['timezone'];?></td>
</tr>
<?php };?>
</table>
你需要改變
<?php while($company=mysqli_fetch_array($result)){ ?>
引用創建的mysqli結果$query
:
<?php while($company=mysqli_fetch_array($query)){ ?>
您可以使用面向對象
<?php
include('session.php');
if ($_SESSION['login_user']){
include 'includes/header.php';
$query = "SELECT * FROM companytable ";
if(isset($_POST['search_box'])){
$search_term = $db->real_escape_string($_POST['search_box']);
$query.=" WHERE title ='{$search_term}'
OR country ='{$search_term}'
OR description ='{$search_term}'
OR timezone ='{$search_term}';";
}
if(!$s = $db->query($query)){
die($db->error);
}
}
表
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>Address</th>
<th>Country</th>
<th>Time Zone</th>
</tr>
<?php while($m = $s->fetch_object()){ ?>
<tr>
<td data-th="ID"><?=$m->id;?></a></td>
<td data-th="Name"><?=$m->title;?></td>
<td data-th="Description"><?=$m->description;?></td>
<td data-th="Type"><?=$m->type;?></td>
<td data-th="Address"><?=$m->address;?></td>
<td data-th="Country"><?=$m->country;?></td>
<td data-th="Time Zone"><?=$m->timezone;?></td>
</tr>
<?php
};
$s->free();
?>
</table>
因此,事實證明,我決定使用sortable.js。它不僅要進行搜索,而且還要通過單擊標題對行進行排序。 因此,如果您要尋找一個完整的修復程序,那就足夠了。
形成
<form class="form" name="search_form"> <input id="search" style="" class="form-control" type="text" name="search" placeholder="🔍 Search...">
表
<table id="table" class="sortable" >
<thead style="cursor:pointer;">
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>Address</th>
<th>Country</th>
<th>Time Zone</th>
</thead>
<tbody>
<?php while($company=mysqli_fetch_array($result)){ ?>
<tr>
<td data-th="ID" sorttable_customkey="2"><?=$company['id'];?></a></td>
<td data-th="Name"><?=$company['title'];?></td>
<td data-th="Description"><?=$company['description'];?></td>
<td data-th="Type"><?=$company['type'];?></td>
<td data-th="Address"><?=$company['address'];?></td>
<td data-th="Country"><?=$company['country'];?></td>
<td data-th="Time Zone"><?=$company['timezone'];?></td>
</tr>
<?php };?>
</tbody>
<tfoot></tfoot>
</table>
搜索腳本
<script src="js/sorttable.js"></script> //load sortable.js
<script type="text/javascript">
var $search_rows = $('#table tr');
$('#search').keyup(function() {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
$search_rows.show().filter(function() {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.