PDO未执行语句

时间:2016-01-13 10:40:57

标签: php mysql oop pdo

我有一个简单的方法,它不能正常工作 - 想要使用预处理语句,但不知何故它没有被执行;相反,原始查询工作正常。

可能是什么问题?我应该将一些额外的args传递给pdo方法吗?

$_POST['sequence'] = [
    0 => 2,
    1 => 1
];

if (!empty($_POST['sequence'])) {
    $query = '
        UPDATE '.$this->db->backtick($this->controller->table).'
        SET `sequence` = CASE `id`'
        ;

    foreach ($_POST['sequence'] as $sequence => $id) {
        $values[':id'.$id] = $id;
        $values[':sequence'.$sequence] = $sequence;
        $query .= ' WHEN :id'.$id.' THEN :sequence'.$sequence;
    }

    $values[':ids'] = implode(',', array_values($_POST['sequence']));

    $query .= ' END WHERE `id` IN (:ids)';

    $statement = $this->db->handle->prepare($query);
    $statement->execute($values); //doesn't work

    //$query2 = str_replace(array_keys($values), array_values($values), $query);
    //$this->db->handle->query($query2); works
}

1 个答案:

答案 0 :(得分:0)

不要将IN

中的参数绑定
$query .= ' END WHERE `id` IN ('.implode(',', array_values($_POST['sequence'])).')';