2016-05-31 90 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