AutoSuggest使用PHP / MySQL和Ajax

| 我已经使用PHP / MySQL和Ajax创建了Autosuggestion,但是当我单击带有(\')的搜索建议时,它不会填满搜索框,但其他所有内容都会填满。例如,我不能单击显示“只是不能得到足够的”的搜索结果,但是可以单击“只是不能得到足够的”。你们能告诉我为什么吗? 谢谢 代码:
<?php
    include(\'conn2.php\');
    $str = strtolower($_GET[\'content\']);         
    if(strlen($str))
    {
        $sel = mysql_query(\"SELECT DISTINCT title FROM Music WHERE title LIKE \'\".mysql_real_escape_string(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>\";
    }
?>
    
已邀请:
        
$str = htmlentities(strtolower($_GET[\'content\'])); 
另外,如果使用新的PHP版本,则可以使用新的filter_input函数来防止SQL注入。 仅供参考: http://php.net/manual/zh/function.filter-input.php     
        我认为问题可能在于搜索结果中的单引号与下面的HTML行中的单引号冲突,该HTML行使用单引号将参数封装在鼠标悬停/移出/单击时的函数调用中。
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\";
正如您将看到的那样,highlight()和display()函数都将title参数用单引号引起来,并且标题中的单引号可能会与它们冲突,从而破坏HTML。尝试转义$ row [\'title \']中的单引号,然后它应该可以工作。 希望这可以帮助!     

要回复问题请先登录注册