我對列表(view1)中的每一行都有一個編輯按鈕。點擊該編輯按鈕時,會打開一個網站,在其中編輯行字段(視圖2)。它將行的id正確地傳遞給view2(在下面的例子中是id11),這個uri段我在模型中調用,用來自控制器的數組數據更新第11行,該數組數據也傳遞給模型。但它仍然不更新。感謝您的幫助。Codeigniter通過在uri中傳遞id來更新數據庫行
(圖1):
//clicking edit button - redirects and passes row id 11 to view 2
<a href='<?= site_url("admin/admins/update_site/$admin->id") ?>'><img src='<?= base_url('media/admin/images/icons/config.png'); ?>'/></a>
(圖2):
// view 2 opens where I can edit the row fields
//url is now: www.site.com/admin/admins/update_site/11:
<form action="<?= site_url('admin/admins/update'); ?>" method="POST">
Username: <br>
<input type='text' name='username'/><br>
Password: <br>
<input type='password' name='password' /><br>
<input type='submit' name='submit' value='Save' />
</form>
控制器:
//making an array with updated data and send it to the model
public function update(){
$update = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$this->load->model('admin/admins_model');
$this->admins_model->update_admin($update);
redirect(site_url('admin/admins/read'));
}
型號:
//update row 11 (from uri segment) the with the array data from controller
public function update_admin($update){
$this->db->where('id', $this->uri->segment(4));
$this->db->update('admins', $update);
}