2012-02-03 106 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