ajax搜索一直说“没有建议”

时间:2012-02-03 17:43:08

标签: php html ajax

我正在尝试进行实时搜索以显示业主用户名,这是我的代码:

PHP文件“f_searchBoForAd.php”

include('functions_cp/f_connection.php');
sqlconnection();

$getName_sql = 'SELECT * FROM businessowner
WHERE businessOwnerUserName LIKE "%' . $searchq .'%"';
$getName = mysql_query($getName_sql) or die (mysql_error());

$a=mysql_fetch_array($getName);

//get the q parameter from URL
$q=$_POST["q"];

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
  {
  $hint="";
  for($i=0; $i<count($a); $i++)
    {
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
      {
      if ($hint=="")
        {
        $hint=$a[$i];
        }
      else
        {
        $hint=$hint." , ".$a[$i];
        }
      }
    }
  }

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
  {
  $response="no suggestion";
  }
else
  {
  $response=$hint;
  }

//output the response
echo $response;

Javascript文件,ajax_framework.js

function showHint(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST","f_searchBoForAd.php?q="+str,true);
xmlhttp.send();
}

我的html表单的结果一直说“没有建议”,问题出在哪里?

2 个答案:

答案 0 :(得分:1)

f_searchBoForAd.php中,事情没有以正确的顺序发生。考虑这个过程:

  1. 从查询字符串中获取搜索参数
  2. 清理搜索参数,以便您的数据库不会被黑客入侵
  3. 查询数据库以获取建议
  4. 返回结果,如果有的话
  5. 考虑到这一点,您的基本php将如下所示:

        $q=mysql_real_escape_string(isset($_POST['q']) ? $_POST['q'] : false);
    
        if (strlen($q) < 1)
            die('Invalid query');
    
        $getName_sql = '
            SELECT 
                * 
            FROM 
                businessowner 
            WHERE 
                businessOwnerUserName LIKE "%' . $q .'%"
        ';
        $getName = mysql_query($getName_sql) or die ('Database error:'.mysql_error());
    
        $output = '';
        while ($results_row=mysql_fetch_assoc($getName)) {
            // assemble your results here... 
            $output .= '<span>'.$results_row['name'].'</span>';
        }
       die($output);
    

    此外,在查询数据库时,应指定要提取的字段而不是SELECT *...。如果您需要id和名称,请使用SELECT id, name... - 这样您的代码将在未来显示给其他人或您自己,并且您可以轻松地调试意外结果。如果您使用*,则结果可能不包含代码使用的字段。如果您指定了字段,并且您希望其中一个字段不存在,那么您将在查询中收到数据库错误,该错误可以准确解释发生的情况。

答案 1 :(得分:1)

我可以在PHP代码上看到一些问题:

  1. 您只查看从SQL查询返回的第一个结果,您应该使用以下代码循环结果:

    while($a=mysql_fetch_array($getName)) {
        // do something with each row, which is $a
    }
    

    您拥有它的方式实际上是在第一行循环列(使用for($i=0; $i<count($a); $i++)循环,并在$a[$i]中查找值)

  2. 在SQL查询中,您将查找包含搜索条件的记录,并被任何内容(LIKE "%' . $searchq .'%"')包围。然后,在PHP循环中,您似乎正在检查搜索词开头的记录,在这里:

    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))

    您可以使用LIKE "' . $searchq .'%"'

  3. 在SQL查询本身中执行此操作
  4. 同样在SQL部分,您似乎在声明或分配值之前使用$searchq。在构建SQL查询之前,您应该具有以下内容:

    $searchq = mysql_real_escape_string($_POST['q']);
    

    否则,您的查询将返回db表中的所有行。