//=====================================
// models
//=====================================
public function edit($id, $title, $content)
{
//設值
$this->db->set('title', $title);
$this->db->set('content', $content);
//指定要更新的id
$this->db->where('id', $id);
//存入資料庫
$this->db->update('news');
//產生: UPDATE news SET title = '$title', content = '$content' WHERE id = $id
}
方法二
//=====================================
// Controller
//=====================================
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
//=====================================
// models
//=====================================
public function edit($id, $data)
{
$this->db->where('id', $id);
$this->db->update('mytable', $data);
}
方法三
//=====================================
// Controller
//=====================================
public function modify()
{
//id
$class_item_id = (int)$this->input->get('class_item_id', TRUE);
//post資料
$data = array(
'class_item_name' => $this->input->post('class_item_name'),
'class_item_sort' => $this->input->post('class_item_sort'),
'class_item_check' => $this->input->post('class_item_check')
);
//產生sql
$where = "class_item_id = ".$class_item_id;
$str = $this->db->update_string('class_item', $data, $where);
//執行model
$this->sql_class_item->update($str);
redirect('manage/class_item');
}
//=====================================
// models
//=====================================
public function update($data){
$this->db->query($data);
}
註:update字串產生
$data = array('name' => $name, 'email' => $email, 'url' => $url);
$where = "author_id = 1 AND status = 'active'";
$str = $this->db->update_string('table_name', $data, $where);
產生:UPDATE table_name SET name = 'Rick', email = 'rick@example.com', url = 'example.com' WHERE author_id = 1 AND status = 'active'
沒有留言:
張貼留言