CodeIgniter - Form: Updating Data
CodeIgniter Form Step By Step Tutorial - Part 18: After create edit form, now we add updating function.
First, create entry_update()at model.
function entry_update(){
$this->load->database();
$data = array(
'title'=>$this->input->post('title'),
'author'=>$this->input->post('author'),
'publisher'=>$this->input->post('publisher'),
'year'=>$this->input->post('year'),
'available'=>$this->input->post('available'),
'summary'=>$this->input->post('summary'),
);
$this->db->where('id',$this->input->post('id'));
$this->db->update('books',$data);
}
Then, update our input() at controller:
function input($id = 0){
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('books_model');
if($this->input->post('mysubmit')){
if($this->input->post('id')){
$this->books_model->entry_update();
}else{
$this->books_model->entry_insert();
}
}
$data = $this->books_model->general();
if((int)$id > 0){
$query = $this->books_model->get($id);
$data['fid']['value'] = $query['id'];
$data['ftitle']['value'] = $query['title'];
$data['fauthor']['value'] = $query['author'];
$data['fpublisher']['value'] = $query['publisher'];
$data['fyear']['value'] = $query['year'];
if($query['available']=='yes'){
$data['favailable']['checked'] = TRUE;
}else{
$data['favailable']['checked'] = FALSE;
}
$data['fsummary']['value'] = $query['summary'];
}
$this->load->view('books_input',$data);
}
If there is id that be sent by post, it will load entry_update (line 9).
