2012-02-03 89 views
0

我试图做一个Live搜索的展现企业的所有者用户名,这里的所有时间是我的代码: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表单的结果说“不建议”所有的时间,问题出在哪里?

+0

$ searchq从哪里来? – BNL 2012-02-03 17:50:02

回答

1

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

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

  1. 你仅仅看到的第一个结果从SQL查询,你应该用代码循环的结果是这样返回

    你的方式,你实际上是循环的第一行上的列(与for($i=0; $i<count($a); $i++)循环,并在$a[$i]寻找值)

  2. 在SQL查询中,查找包含包含条款的记录,并且包含任何内容(LIKE "%' . $searchq .'%"')。然后,在PHP循环,你似乎在检查该搜索词,在这里开始记录:

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

    你可以做的是,在SQL查询本身,通过使用LIKE "' . $searchq .'%"'

  3. 同样在SQL部分,您似乎在声明或分配值之前使用$searchq。在构建SQL查询之前,您应该有如下所示的内容:

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

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

1

f_searchBoForAd.php,事情并没有按照正确的顺序发生。想想这个过程:

  1. 获取从查询字符串搜索参数
  2. 消毒搜索参数,以便你的数据库没有得到攻入
  3. 查询数据库中的建议
  4. 返回结果,如果有的话

考虑到这,你的基本的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... - 这样,您的代码将来会明确给其他人或您自己,并且您可以轻松调试意外的结果。如果您使用*,您的结果可能不包含您的代码使用的字段。如果您指定了字段并且您希望的字段之一不存在,那么您将在查询中得到一个数据库错误,以准确解释发生了什么。

+0

谢谢,'$ a'应该用'$ results'替换吗? – 2012-02-03 18:19:17

+0

是的。会纠正。下来选民 - 请解释。 – 2012-02-03 18:57:26