如何修复搜索栏错误

时间:2017-08-21 19:42:38

标签: php search mysqli

我想为网站实现搜索功能并使用以下代码: -

HTML: -

 <form action="" method="get" class="navbar-form" >
 <div class="input-group">
 <input type="text" class="form-control" placeholder="Search Post" name="kw"/>
 <div class="input-group-btn">
 <button class="btn btn-default" type="submit" value="Search"/><i class="glyphicon glyphicon-search clearable__clear"></i></button>
 </div>
 </div>
 </form> 

&安培; PHP代码: -

 <?php
 if(!empty($_GET['kw'])){
 $ss=$_GET['kw'];
 $query="select * from mydb where title like '%$ss%'";
 $result=mysqli_query($connect,$query);
 while($row=mysqli_fetch_assoc($result));
 }
 ?>

当我点击搜索按钮时,搜索栏无效。网址正在这样改变 http://localhost/project/sports.php?kw=match但没有任何效果。请让我知道如何解决它?

1 个答案:

答案 0 :(得分:0)

这里是您的问题的工作代码,但根据安全问题/ sql注入,此查询易受攻击, 请访问How can I prevent SQL injection in PHP?了解详情。

<?php
if(!empty($_GET['kw'])){
    $ss=$_GET['kw'];
    $connect = mysqli_connect('localhost','restau', '12345', 'restau');

    $result = mysqli_query($connect,$query);
    $i = 1;
    echo '<table>'; // i have used here table, create your design as you like
    while ($row = mysqli_fetch_assoc($result)) {
        echo '<tr>';
        echo '<td>'. $i.'</td>';
        echo '<td>'. $row['title'].'</td>';
        echo '</tr>';
        $i++;
    }
    echo '</table>';
}
?>