2017-02-10 295 views
1

我写了一个简单的页面脚本来循环访问数据库中的数据的动态表。但是,当我点击搜索页面返回空白,没有错误。我试图理解没有成功的情况。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> 

回答

1

您需要更改

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

引用的mysqli结果您创建,$query

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

相同空白页的回报。 – Skynet

+0

您确认您正在创建的'$ searchQ'在结果中有任何行吗? – wogsland

+0

好吧,我用'$ searchQ =“SELECT * FROM companytable LIMIT 5”;'来测试一次是否只返回5行并且它工作。所以我猜是的。 – Skynet

1

您可以使用面向对象的

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

现在我得到这个致命错误:不能使用stdClass类型的对象作为数组 – Skynet

+0

@Skynet现在检查,我改变了一些变量。你使用的是哪个版本的php和mysql? –

+0

我正在使用5.6版本。 – Skynet

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>