2012-01-18 96 views
0

我们的搜索建议存在问题。每次我们点击我们网站上的建议时,它都会在搜索查询前放置一个空格,导致查询失败。搜索建议框在搜索查询前输入空格

,我们使用的建议的代码是这样的:

$query = $db->query("SELECT DISTINCT productnaam FROM product WHERE merk LIKE  '$queryString%' LIMIT 10"); 
      if($query) { 
       // While there are results loop through them - fetching an Object (i like PHP5 btw!). 
       while ($result = $query ->fetch_object()) { 
        // Format the results, im using <li> for the list, you can change it. 
        // The onClick function fills the textbox with the result. 

        // YOU MUST CHANGE: $result->value to $result->your_colum 
        echo '<li onClick="fill(\''.$result->merk.'&nbsp;'.$result->productnaam.'\');">' 
        .$result->merk.'&nbsp;'.$result->productnaam.''.'</li>'; 
       } 
      } else { 
       echo 'ERROR: There was a problem with the query.'; 
+0

'$ queryString'从哪里来? (显示代码) – greut

回答

0

试试用装饰()

$queryString = trim($queryString); 

装饰()函数从两侧的空格和其他预定义字符一个字符串。

+0

这是我的查询字符串来自:(我已添加修剪,但不起作用) //是否有发布的查询字符串? \t \t如果(isset($ _ POST [ '的queryString'])){ \t \t \t $的queryString =修剪($ DB-> real_escape_string($ _ POST [ '的queryString'])); – Forza

0

尝试下面指定的trim()函数为Sameera Thilakasiri,并且还将您的查询更新为类似"SELECT DISTINCT productnaam FROM product WHERE merk LIKE '%$queryString%' LIMIT 10"这两个百分号将确保您的查询将选择包含您的输入的记录,而不是以您的输入开始的记录。

波纹管在SQL LIKE条件的一些进一步的解释,可以帮助你走出

// This query will look for records that start with "sa"  
select * from table where name like 'sa%' 

// This query will look for records that contain "sa"  
select * from table where name like '%sa%' 

// This query will look for records that end with "sa"  
select * from table where name like '%sa' 

希望帮助!