【发布时间】:2011-09-17 15:15:12
【问题描述】:
所以我使用 PHP/MYSQL 和 Ajax 为我的搜索引擎创建了一个类似于 google 的自动提示。如果我的 MySQL 有 2 个不同的同名标题,我如何让其中一个出现在自动建议中?例如:我有一个带有 title= ufc 131 的字段和另一个带有 title=ufc 131 的字段。当我搜索 UFC 131 时,我如何只显示其中一个?
我使用的代码是..
<?php
include('conn.php');
$str = strtolower($_GET['content']);
if(strlen($str))
{
$sel = mysql_query("select * from Streams where title like '".trim($str)."%'");
if(mysql_num_rows($sel))
{
echo "<table border =\"0\" width=\"100%\">\n";
if(mysql_num_rows($sel))
{
echo "<script language=\"javascript\">box('1');</script>";
while($row = mysql_fetch_array($sel))
{
$country = str_ireplace($str,"<b>".$str."</b>",($row['title']));
echo "<tr id=\"word".$row['title']."\" onmouseover=\"highlight(1,'".$row['title']."');\" onmouseout=\"highlight(0,'".$row['title']."');\" onClick=\"display('".$row['title']."');\" >\n<td>".$country."</td>\n</tr>\n";
}
}
echo "</table>";
}
}
else
{
echo "<script language=\"javascript\">box('0');</script>";
}
?>
【问题讨论】:
-
你的脚本很容易受到sql injection attacks的攻击
-
嗯?对不起,我是 sql 注入的新手
-
永远不要相信用户输入。在查询中使用它们之前总是从用户那里转义字符串,并且在将它们显示给用户之前总是对字符串进行转义。使用
mysql_real_escape_string()。这与您当前的问题无关,只是一个警告。 -
我支持这个。例如,如果用户在“搜索字符串”之后键入,它将删除整个搜索建议表。:“'; DELETE * FROM Streams” 所以,考虑到这有多简单,想象一下这会发生什么其他疯狂。
标签: php mysql ajax autosuggest