创建search.php
,创建数据库war
和表product
。
我的代码不工作:
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="get" action="">
<table>
<tr>
<td>متن برای جستجو</td>
<td><input type="text" name="text"></td>
</tr>
<tr>
<td><input type="submit" value="Search"></td>
<td>
<?php
if(isset($_GET['text']) && !empty($_GET['text'])){
$body=$_GET['text'];
$con=mysql_connect("localhost","root","");
if(!$con){die("mysql Error");}
if (!mysql_select_db("war",$con)){die("mysql select error");}
$res=mysql_query("SELECT * FROM product WHERE LIKE pname='%$body%'");
$count=mysql_num_rows($res);
if ($count <= 0){
die("Your product Not found");
}else{
while ($row=mysql_fetch_array($res)){
echo $row['pname'];
}
}
}
?>
</td>
</tr>
</table>
</form>
</body>
</html>
请帮忙调试
您的MySQL
SELECT语句是错误的。应该是:
$res=mysql_query("SELECT * FROM product WHERE pname LIKE '%$body%'"); // note the absence of =
当然,如果这就是你所说的"it doesn't work"。
PS: do not use mysql_*
functions