2016-08-24 57 views
0

我嘗試使用Codeigniter的查詢構建器類更新我的「設置」表。 這是我的php代碼:Codeigniter中的數據庫錯誤

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

+0

依次運行您的查詢 –

+0

是啊!但我認爲這種方法是好的,因爲當我逐一運行更新查詢時,它會進行多個數據庫調用。 – Great9

+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')