我希望根据复选框选择,使用或不使用多个类别进行mysqli全文搜索。
搜索基于同一列中的四个类别。
搜索类别包括Windows,游戏,平板电脑,手机。
通过复选框选择搜索选择的类别/多个类别应仅针对所选类别进行。
表格结构
id category title date ...
1 windows windows7 d1 ...
2 games windows games d2 ...
3 tablet windows tablet d3 ...
4 mobile windows mobile d4 ...
5 windows windows8 d5 ...
6 windows windows vista d6 ...
搜索复选框
搜索
<li><label><input name="all" type="checkbox" checked="checked" />All</label></li>
<li><label><input name="windows" type="checkbox" />Windows OS</label></li>
<li><label><input name="mobile" type="checkbox" />Windows Mobile</label></li>
<li><label><input name="games" type="checkbox" />Windows Games</label></li>
<li><label><input name="tablet" type="checkbox" />Windows Tablet</label></li>
示例1:
Search for 'windows' should return result as
windows7
windows8
windows vista
When only checkbox windows is checked
示例2:
Search for 'windows' should return result as
windows7
windows8
windows vista
windows games
When checkbox windows and games are checked.
我已经提出了一个查询,但根本没有查询。
$query = "SELECT * FROM $table WHERE ";
$query .= "category='' ";
$query .= "OR category='windows' ";
$query .= "OR category='games' ";
$query .= "OR category='mobile' ";
$query .= "OR category='tablet' ";
$query .= " AND MATCH (title) AGAINST ('+$keyword*' IN BOOLEAN MODE) ORDER by id desc, title desc LIMIT 10";
我也尝试过这样但不起作用。
$query = "SELECT * FROM $table WHERE ";
$query .= "MATCH (category) AGAINST ('' IN BOOLEAN MODE) ";
$query .= "OR MATCH (category) AGAINST ('windows' IN BOOLEAN MODE) ";
$query .= "OR MATCH (category) AGAINST ('games' IN BOOLEAN MODE) ";
$query .= "OR MATCH (category) AGAINST ('mobile' IN BOOLEAN MODE) ";
$query .= "OR MATCH (category) AGAINST ('tablet' IN BOOLEAN MODE) ";
$query .= " AND MATCH (title) AGAINST ('+$keyword*' IN BOOLEAN MODE) ORDER by id desc, title desc LIMIT 10";
请查看其无效的原因,并建议如何对所选字段进行搜索查询。
感谢。
答案 0 :(得分:2)
SELECT * FROM table WHERE MATCH (field1, field2, field3, field4)
AGAINST ('keyword' IN BOOLEAN MODE)
似乎是你需要的。
但是您的查询对SQL注入是开放的。
现在可以回答。所以,这里有一个基于safeMysql的解决方案(因为原始的mysqli需要太多的代码)
$sql = "SELECT * FROM ?n WHERE category IN(?a)
AND MATCH (title) AGAINST (?s IN BOOLEAN MODE) ORDER by id desc LIMIT 10";
$data = $db->($sql,$table,$_GET['category'], $_GET['keyword']);
请注意,您必须将复选框字段称为
<input name="category[]" value="windows" type="checkbox" />