<?php
$username = "root";
$password = "";
$hostname = "localhost";
$db_handle = mysql_connect($hostname, $username, $password) or die ("Could not connect to database");
$selected= mysql_select_db("login", $db_handle);
$output='';
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$query= "SELECT * FROM PHP_Item WHERE Name LIKE '%searchq%' OR Description LIKE '%serachq%'" or die ("could not search");
$result= mysql_query($query);
$count= mysql_num_rows($result);
echo $count;
if($count <1){
$output = 'there were no search results';
}else{
while($row = mysql_fetch_array($query)){
$mName = $row['Name'];
$price = $row['Price'];
$id= $row['ItemID'];
$output .= '<div>'.$mName.' '.$price.'</div>';
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> --><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> --><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> --><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html>
<head>
<title>Movie Search-Search for a movie</title>
</head>
<body>
<form action="search.php" method="POST">
<input type="text" name="search" placeholder="Find movies..."/>
<input type="submit" value="Search movies"/>
</form>
<?php print("$output");?>
</body>
</html>
我试图在我的网站上实现一个搜索栏,用户可以在其中输入电影名称,它将返回与用户搜索名称相同或相似的电影。
正在搜索的数据库有3个字段-->> ItemID、Name、Description。 我不断收到 0 个结果,并显示“没有搜索结果”输出。有什么想法吗?
最佳答案
您的代码存在一些问题。
首先,您省略了变量的美元符号,从技术上来说,您将搜索“searchq”或“serachq”文字字符串。 “serachq”是一个拼写错误,如下所述。
'%searchq%' OR Description LIKE '%serachq%'
按照$searchq = $_POST['search'];
另外,您还输入了“serachq
”一词。在LIKE '%serachq%'
重写:
'%$searchq%' OR Description LIKE '%$searchq%'
- 检查错误就会发现这些错误。
您的or die ("could not search");
在你的查询中,这没有帮助。请参阅下面关于添加 or die(mysql_error())
的注释至mysql_query()
.
然后这一行:
while($row = mysql_fetch_array($query))
应该引用$result
在$result= mysql_query($query);
而不是$query
while($row = mysql_fetch_array($result))
添加error reporting到文件顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告只能在暂存阶段完成,而不能在生产阶段完成。
另添加or die(mysql_error())
至mysql_query()
.
您的 HTML 中也有一些似乎被注释掉的代码,并且已损坏;仔细检查:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> --><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> --><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> --><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
这会破坏您的页面。
只需将整个 block 更改为
<!DOCTYPE html>
您当前的代码已开放给 SQL injection 。使用prepared statements ,或PDO with prepared statements ,它们更安全。
条件语句选项:
这一行:if(isset($_POST['search']))
也可以改为
if(
isset($_POST['search']) &&
!empty($_POST['search'])
)
为了确保输入不留空。
或者简单地说:
if( !empty($_POST['search']) )
关于PHP mySQL 搜索不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29950414/