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).



Series this article:
CodeIgniter - Form: Creating Skeleton
CodeIgniter - Form: File Structure
CodeIgniter - Form: Creating Layout Code
CodeIgniter - Form: Creating Menu Library
CodeIgniter - Form: Putting Text at Header and Footer
CodeIgniter - Form: Centralizing $data
CodeIgniter - Form: Adding CSS
CodeIgniter - Form: Creating Form HTML
CodeIgniter - Form: Creating Beautiful Form Code
CodeIgniter - Form: CodeIgniter HTML Style
CodeIgniter - Form: Preparing Table at Database
CodeIgniter - Form: Creating Insert Data
CodeIgniter - Form: Creating List Data Use Table Library
CodeIgniter - Form: Creating Table List Without Table Library
CodeIgniter - Form: Using URL Helper
CodeIgniter - Form: Showing Single Data for Form Edit
CodeIgniter - Form: Something Happen With Form Parameters
CodeIgniter - Form: Updating Data
CodeIgniter - Form: Deleting Data

Bookmark and Share Tag: CodeIgniter, framework, MVC, database, update Category: PHP Framework Post : March 03rd 2008 Read: 42,205

blog comments powered by Disqus