PHP搜索不起作用

时间:2017-09-17 11:38:35

标签: php html5

我不明白代码的问题是什么。它应该工作。表中有数据,但搜索仍未产生任何结果。在搜索栏中输入任何数据之前或之后,搜索栏保持静止且没有变化。

以下是代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search Example</title>
</head>

<body>
<form action="index.php" method="post">
<input type="text"  name="search" placeholder="Search" />
<input type="submit" value="submit" /> 
</form>

<?php
  $con = mysqli_connect("localhost","root","") or die("Could not connect");
  mysqli_select_db($con ,"project") or die(mysqli_error());

if(isset($_POST['search']))
{
  $search =$_POST['search']; // searchq contains characters which is typed 
  in the search
  $search = preg_replace("#[^0-9a-z]#i","",$search); //filtering the 
  conditions */
  $query = mysqli_query($con, "SELECT * FROM admin WHERE name LIKE 
 '%$search%'");

  //most important condition line for the search
  $count = mysqli_num_rows($query); // To count the selected Rows 
  if($count==0)
  {
        echo"<h2>"."No Data Found"."</h2>";
    }
 else
 {
   while($row = mysqli_fetch_array($query))
 {

 echo "<tr>".
   "<td>".$row['username'] ."</td>".
   "<td>".$row['password'] ."</td>".
     "</tr>";
 }

 }
 }
  ?>

 </body>
 </html>

数据库名称是项目,表名为admin,其中Idusernamepassword为列。

2 个答案:

答案 0 :(得分:0)

像这样使用

$query = mysqli_query($con, "SELECT * FROM `admin` WHERE `username` LIKE 
  '%{$search}%'");  

答案 1 :(得分:0)

这样做:

$ query = mysqli_query($ con,“SELECT * FROM admin WHERE name LIKE  '%{$搜索}%'“);

通过在{}中包含变量,您可以指定只有$ title是变量,双引号字符串将确保此变量扩展为其值。