Codeigniter中的数据库错误

时间:2016-08-24 14:54:05

标签: sql database codeigniter mysqli codeigniter-3

我尝试使用Codeigniter的查询构建器类更新我的'settings'表。 这是我的PHP代码:

public function update_settings($post_array) {

    foreach ($post_array as $setting_name => $setting_value) {

        $this->db->set('setting_value', $setting_value);
        $this->db->where('setting_name', $setting_name);
        $query_parts[] = $this->db->get_compiled_update('settings');

    }

    $full_query = implode(';', $query_parts);

    $this->db->query($this->db->escape($full_query));

    if ($this->db->affected_rows() == 1 || $this->db->affected_rows() == 0) {
        return TRUE;
    } else {
        return FALSE;
    }

}

当我提交设置表单时。波纹管错误正在弹出。

enter image description here

1 个答案:

答案 0 :(得分:0)

根据codeignitor文档,这里是做到这一点的方法。

$data = array(
array(
    'title' => 'My title' ,
    'name' => 'My Name 2' ,
    'date' => 'My date 2'
),
array(
    'title' => 'Another title' ,
    'name' => 'Another Name 2' ,
    'date' => 'Another date 2'
)

);

$this->db->update_batch('mytable', $data, 'title');
// Produces: 
// UPDATE `mytable` SET `name` = CASE
// WHEN `title` = 'My title' THEN 'My Name 2'
// WHEN `title` = 'Another title' THEN 'Another Name 2'
// ELSE `name` END,
// `date` = CASE 
// WHEN `title` = 'My title' THEN 'My date 2'
// WHEN `title` = 'Another title' THEN 'Another date 2'
// ELSE `date` END
// WHERE `title` IN ('My title','Another title')