我编写了一个非常简单的PHP来搜索MySQL数据库中的字段中的记录并返回该字段的所有详细信息。
不幸的是,每当我输入数字来搜索任何内容时,什么都不会回来,甚至没有任何错误。
我的代码如下:
<?php
mysql_connect("localhost", "root", "") or die("cant connect to db");
mysql_select_db("db name")or die("cant connect.....");
$output='';
//collect
if(isset($_POST['search'])){
$searchq=$_POST['search'];
$query = mysql_query("SELECT * FROM register WHERE licence LIKE '%$searchq%'") or die("cant search....");
$count = mysql_num_rows($query);
if($count==0){
$output='no results';
} else {
while($row=mysql_fetch_array($query)){
$fdriver=$row['driver'];
$flicence=$row['licence'];
$fofficer=$row['officer'];
$fspeed=$row['speed'];
$ffine=$row['fine'];
$fcategory=$row['category'];
$output.='<div> '.$fdriver.' '.$flicence.' '.$fspeed.' '.$ffine.' '.$fcategory.'</div>';
}
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Search</title>
</head>
<body>
<h3>Search</h3>
<p>You have to enter your number to search</p>
<form method="post" action="search_start.php" >
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
<?php print("$output"); ?>
</body>
</html>
答案 0 :(得分:1)
您正在检查$_POST['search']
,因此您的文本元素字段的名称应更改为搜索。
<input type="text" name="search">
答案 1 :(得分:1)
首先更改文本框的名称以进行搜索
<input type="text" name="search">
第二个你的查询应该是这样的
$query = mysql_query("SELECT * FROM register WHERE licence LIKE '%".$searchq."%'");