MYSQL在同一列中多次

时间:2013-03-13 14:03:49

标签: php mysql

我有3个下拉列表的过滤器,每个列表包含相同的标准(运营商,服务,SIM卡)。当这些下拉列表包含不同的搜索类别时,搜索功能正常工作,但如果2或3个下拉列表中的类别相同,则过滤器无法正常工作。由于SQL查询不正确:

"SELECT * FROM table WHERE id = " . $_GET['id'] . " AND operator like %tele% AND operator like %bite%"

应该是

"SELECT * FROM table WHERE id = " . $_GET['id'] . " AND operator like %tele% OR operator like %bite%"

这是我的过滤器的PHP代码:

 $this->_sql['where'].= $this->reg['post']['flt_find_1'] ? " AND ".$this->reg['post']['flt_field_1']." like '%".$this->reg['post']['flt_find_1']."%' " : "";      
 $this->_sql['where'].= $this->reg['post']['flt_find_2'] ? " AND ".$this->reg['post']['flt_field_2']." like '%".$this->reg['post']['flt_find_2']."%' " : "";  
 $this->_sql['where'].= $this->reg['post']['flt_find_3'] ? " AND ".$this->reg['post']['flt_field_3']." like '%".$this->reg['post']['flt_find_3']."%' " : "";  

如何检查过滤器中的所有类别是否不同,然后对每个类别使用AND,但是类别是相同的,使用OR(或者可能有其他方法来解决这个问题)

感谢。

1 个答案:

答案 0 :(得分:0)

  1. 您的代码容易受到sql注入攻击,因此请检查get参数;
  2. 如果我理解正确的话,请尝试这样:
  3. SELECT * FROM table WHERE id = '".($_GET['id']*1)."' AND ( operator LIKE '%tele%' AND operator LIKE '%bite%' )";