假设$this->input->post('location')持有这样一个数组:
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'));谢谢!
发布于 2016-05-31 13:04:05
在guide/database/queries.html上看到,是的,这样做是安全的。但你只需要一个“?”
代码应该是这样的:
$sql = "SELECT id
FROM location
WHERE id IN (?)";
$locations = $this->db->query($sql, $this->input->post('location'));发布于 2016-05-31 10:45:28
我不确定这是否值得回答,但我还是这么做了,是的,正如alex在评论中所说的那样,您的查询是安全的,但我不理解str_repeat的不必要的复杂性--我不确定,但CI中有其他方法可以写出这样的查询:
$query = $this->db
->select("id")
->from("location")
->where_in("id",$this->input->post("location"))
->get();上面的查询也是这样做的。我是忽略了这里的一些东西,还是您只是不知道内置的查询生成器?
https://stackoverflow.com/questions/37535523
复制相似问题