0

我搜索适合搜索查询的关键字。并且信息得到回响。它就像一个搜索引擎。但我似乎无法让它适用于数据库中带有空格的关键字。我已经搜索了一下,但似乎无法让它工作。

<?php

// -- Database Connection --

if (isset($_POST['search_query'])) {
$search_query = mysql_real_escape_string(htmlentities($_POST['search_query']));
echo "<div class=\"searchText\">Search</div><hr />";

//explode the search term
$search_query_x = explode(" ",$search_query);

foreach($search_query_x as $search_each) {
$x++;
if($x==1)
$construct .="keywords = '$search_each'";
}
$construct ="SELECT * FROM search WHERE $construct";
$run = mysql_query($construct);

$foundnum = mysql_num_rows($run);
if ($foundnum==0) {
echo "Sorry, there are no matching result for <b>$search_query</b>.</br></br>1. 
Try more general words.</br>2. Try different words with similar
 meaning</br>3. Please check your spelling";
} else
{
echo "$foundnum results found !<p>";
while($runrows = mysql_fetch_assoc($run))
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];
echo "
<div class='width: 400px;'>
<div class='title'><a href='$url'><b>$title</b></a></div>
<div class='url'>$url</div>
<div class='desc'>$desc</div>
</div>
<br />
";
}
}
}
else
{
echo "An ERROR HAS OCCURED ...";    
}
?>
4

2 回答 2

1

由于$search_query_x = explode(" ",$search_query);空格分割,您的空格会被删除。

您必须选择一个新的分隔符来分隔数据 - 就像逗号而不是空格。

于 2013-06-28T14:21:36.213 回答
0
$search_query = mysql_real_escape_string(htmlentities($_POST['search_query']));

导致您的脚本将空格更改为与 URL 兼容的 %20。摆脱 htmlentities 功能(此时并不能真正提高安全性),它应该可以正常工作。

当呈现来自不安全来源的信息时,应使用 Htmlentities 函数。

在向用户显示信息时,请务必调用(首选)htmlspecialchars 或 htmlentities,以防止跨站脚本 (XSS)。

此外,评论中还指出您正在使用过时的功能。

于 2013-06-28T14:22:42.770 回答