Codeigniter 3 SQL 注入查询

Codeigniter 3 SQL Injection query

假设 $this->input->post('location') 包含这样一个数组:

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

这个查询"Sql Injection"安全吗?

$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'));

谢谢!

我不确定这是否值得回答,但无论如何我都会这样做, 是的,你的查询是安全的,就像 alex 在评论中说的那样,但我不明白的是 str_repeat 不必要的复杂性 - 我不确定,但 CI 中有其他选择来写下这样的查询那:

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

上面的查询也可以完成这项工作。我是不是忽略了这里的某些内容,还是您只是不知道内置的查询生成器?

http://www.codeigniter.com/user_guide/database/queries.html 上看到的 是的,这样做是安全的。但是你只需要一个'?'。

所以代码应该是这样的:

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