Codeigniter查询条件字符串中的符号

时间:2012-11-14 19:17:22

标签: php mysql codeigniter activerecord

我正在尝试在CMS中使用过滤器下拉列表 我的模型看起来像

public function load($sort,$order,$key,$value)
{ //        $key='listening'; //        $value="1";
            //configure pagination
    $config=array(
      'base_url'=>base_url().'/index.php/companies/index',
      'total_rows'=>$this->db->get('company')->num_rows(),
      'per_page'=>$this->settings_model->get_per_page(),
      'num_links'=>20
    );

    $this->pagination->initialize($config);



    $this->db->select('company.id, 
                       company.name, 
                       company.logo, 
                       company.status_id, 
                       company.listening',FALSE);
    $this->db->select('company_category.name as category,
                       company_category.id as category_id',FALSE);

    $this->db->select('complain_status.cs_status as status',false);
    $this->db->from('company');
    $this->db->join('company_category','company_category.id = company.category_id');
    $this->db->join('complain_stastus', 'complain_status.cs_id = company.status_id');



    if(isset($_POST['key']))
    {
       $value=  str_replace('&nbsp', ' ', $_POST['value']);
        var_dump($value);
        if($value!='0')
            $this->db->having ($_POST['key'], mysql_real_escape_string($value) );

    }
    if($sort!='' || $sort!=NULL)
        $this->db->order_by ($sort, $order);

    $this->db->limit($config['per_page'], $this->uri->segment(3));

    $result=$this->db->get();
    if(!isset($_POST['key']))
        $this->filter->set_filters_list($result->result_array());

    return $result->result();
}

生成以下查询

SELECT company.id, company.name, company.logo, company.status_id, company.listening, company_category.name as category, company_category.id as category_id, complain_status.cs_status as status
FROM (`company`)
JOIN `company_category` ON `company_category`.`id` = `company`.`category_id`
JOIN `complain_status`  ON `complain_status`.`cs_id` = `company`.`status_id`
HAVING `category` =  'Health & recreation'
LIMIT 20

正如你在这里看到的那样,当类别等于某些特殊字符的字符串如Health & recreation时它会失败,即使我尝试了CI生成的查询它在MYSQL上正常工作并得到结果

注意:我正在替换空格$value= str_replace('&nbsp', ' ', $_POST['value']);,因为这个数据来自于选项html元素,当它在选项中有空格时失败,所以我必须在后端代码中解析并删除它

提前致谢

2 个答案:

答案 0 :(得分:2)

代码点火器可能是html_encoding&符号,因此它读取为其html值。您可以通过将此行添加到运行查询的任何控制器或模型的构造函数中来打开探查器来确认这一点:

$this->output->enable_profiler(TRUE);

如果我是对的,那么您的查询将取代&应该是&的内容。

请注意,使用&时,探查器会显示$this->db->last_query(),但仍会显示&

答案 1 :(得分:0)

要将符号插入数据库,您需要先转义值。在PHP中,通常使用:mysql_real_escape_string()

How to insert special characters into a database?

但是,正如您在CodeIgniter中所做的那样,您必须使用查询绑定来自动转义数据,

$category = $this->input->post('category');
$status = $category = $this->input->post('status');
$status_id = $category = $this->input->post('status_id');

$sql = "SELECT * FROM company WHERE category = ? AND status = ?"; 

$this->db->query($sql, array($category, $status));

http://ellislab.com/codeigniter/user_guide/database/queries.html(在查询绑定下)

Does CodeIgniter automatically prevent SQL injection?

虽然它不是原始问题的一部分,但是您的代码存在缺陷,因为您使用$ _POST ['value']而没有任何过滤。什么都没有阻止SQL注入你的表单的人。

How can I prevent SQL injection in PHP?