2016-05-31 95 views
2

Supose是$this->input->post('location')拥有一个这样的数组:笨3 SQL注入查询

Array 
(
    [0] => 0 
    [1] => 1 
    [2] => 2 
    [3] => 3 
    [4] => 4 
    [5] => 5 
    [6] => 6 
    [7] => 7 
) 

这是查询 “SQL注入” 安全吗?

$in = str_repeat('?,', count($this->input->post('location')) - 1) . '?'; 
$sql = "SELECT id 
     FROM location 
     WHERE id IN ($in)"; 
$locations = $this->db->query($sql, $this->input->post('location')); 

谢谢!

+1

是的,这是非常安全 – Alex

回答

2

我不确定这是否值得回答,但我正在做它, 是你的查询是安全的,就像亚历克斯在评论中说的,但我不明白的是与str_repeat不必要的复杂性 - 我'm不确定,但是在CI中有替代方法来写下这样的查询:

$query = $this->db 
      ->select("id") 
      ->from("location") 
      ->where_in("id",$this->input->post("location")) 
      ->get(); 

上面的查询也完成了这项工作。我在这里忽略了什么,或者你只是不了解内置的查询生成器?

+0

THKS很多sintakonte!我不知道这个更好的解决方案。请再次:) – random425

+0

这应该是被接受的答案。 – catbadger