Php MysQl搜索脚本返回空白页面

时间:2017-02-10 22:56:35

标签: php mysql

我编写了一个简单的页面脚本来循环遍历从数据库获取数据的动态表。但是当我点击搜索时,页面返回空白而没有错误。我试图了解发生了什么没有成功。

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="&#128269;"> 
    </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>

3 个答案:

答案 0 :(得分:1)

您需要更改

<?php while($company=mysqli_fetch_array($result)){ ?>

引用您创建的mysqli结果$query

<?php while($company=mysqli_fetch_array($query)){ ?>

答案 1 :(得分:1)

您可以使用面向对象的

=IF(AND(COUNTIF($F$5:$F$22, $H$1)>0,$F5<=$H$1), 1, IF(AND(COUNTIF($G$5:$G$22,$H$1)>0,$G$5<=$H$1),true, false))

表格

<?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);
   }

}

答案 2 :(得分:0)

结果我决定选择sortable.js它不仅负责搜索,还负责通过点击标题对行进行排序。因此,如果您正在寻找一个干净的解决方案,那就是它。

<强>形式

  <form class="form"  name="search_form">  <input  id="search" style="" class="form-control"  type="text" name="search" placeholder="&#128269; 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>