title: how to edit and insert data from the single form
body: <p>This is my controller. Here the function <code>add_post()</code> is the function that is used in both adding and updating data. The problem is that if I want to edit the form then I can get the value from the database into the form of respective area but when I want to add the data using the same function I cannot. I searched a lot but could not find the solution.</p>
  
  <pre><code>    &lt;?php
      class Blog_controller extends CI_Controller {
  
          public function index() {
              $this-&gt;get_post();
          }
  
          public function __construct() {
              parent::__construct();
              $this-&gt;load-&gt;helper('form');
              $this-&gt;load-&gt;helper('url'); 
              $this-&gt;load-&gt;model('blog_model');
          }
  
          function add_post($id) {
              if($id==NULL) {
                  return $this-&gt;load-&gt;view('admin/add_post');
              } else {
                  $data['query']=$this-&gt;blog_model-&gt;get_single_post($id);
                  $this-&gt;load-&gt;view('admin/add_post',$data);
              }
          }
  
          function do_upload() {
              $config['upload_path'] = './uploads/';
              $config['allowed_types'] = 'gif|jpg|png';
              $config['max_size'] = '100';
              $config['max_width']  = '1024';
              $config['max_height']  = '768';
              $this-&gt;load-&gt;library('upload', $config);
  
              if ( ! $this-&gt;upload-&gt;do_upload())
              {
                  $error = array('error' =&gt; $this-&gt;upload-&gt;display_errors());
                  $this-&gt;load-&gt;view('premium/main', $error);
              } else {
                  $this-&gt;upload-&gt;do_upload();
                  $data=$this-&gt;upload-&gt;data();
                  $filename=$data['file_name'];
                  $this-&gt;blog_model-&gt;insert_post($filename);
              }
          }
  
          function get_post() {
              $data['query']=$this-&gt;blog_model-&gt;get_post();
              $this-&gt;load-&gt;view('premium/main',$data);
          }
  
          function view_table() {
              $data['query']=$this-&gt;blog_model-&gt;get_post(); $this-&gt;load-&gt;view('admin/view_table',$data);
          }   
      }
      ?&gt;
  
  
  
  MODEL
  </code></pre>
  
  <p>This is the model of my project.. I want to insert and update the data using the single function i.e. insert_post().</p>
  
  <pre><code>    &lt;?php 
  Class Blog_model extends CI_Model
  {
  
  function __construct()
  {
  parent::__construct();
  $this-&gt;load-&gt;database();
  }
  
  
  
  
   function insert_post($filename)
   {
   $data=array(
   'title'=&gt;$this-&gt;input-&gt;post('title'),
  'description'=&gt;$this-&gt;input-&gt;post('description'),
  'publish'=&gt;$this-&gt;input-&gt;post('publish'),
  'image'=&gt;$filename,
  );
  
  $this-&gt;db-&gt;insert('post',$data);
   redirect('blog_controller');
  
  }
  
  
  function get_post()
  {
  $query = $this-&gt;db-&gt;get_where('post');
  return $query-&gt;result();
  }
  function get_single_post($id)
  {
  $query = $this-&gt;db-&gt;get_where('post',array('id'=&gt;$id));
  return $query-&gt;row($id);
  }
  }
  ?&gt; 
  </code></pre>
  
  <p>This is my view. In this form i want to add the data when the user wants to insert data and when user wants to edit then the data should be retrived in the respective field of the form and should be updated...</p>
  
  <pre><code>    &lt;html&gt;
      &lt;body&gt;
      &lt;h1&gt;Add Blog Title&lt;/h1&gt;
      &lt;?php function add_post($id);?&gt;
      &lt;?php echo form_open_multipart('blog_controller/do_upload');?&gt;
      &lt;p&gt;Title : &lt;input type="text" name="title" value="&lt;?php if($id==0){echo $query-&gt;title;}?&gt;"&gt;&lt;/br&gt;&lt;/p&gt;
      &lt;p&gt;Description : &lt;textarea name="description" rows="5"&gt; &lt;?php echo $query-&gt;title;?&gt;&lt;/textarea&gt;&lt;/br/&gt;&lt;/p&gt;
      &lt;p&gt;Current Image : &lt;img src="&lt;?php echo base_url();?&gt;/uploads/&lt;?php echo $query-&gt;image;?&gt;"&lt;/b&gt;&lt;/p&gt;
      &lt;p&gt;Image : &lt;?php echo form_upload('userfile');?&gt;&lt;/b&gt;&lt;/p&gt;
      &lt;p&gt;Publish ?: &lt;input type="checkbox" name="publish" value="1" &lt;?php if($query-&gt;publish == 1) echo 'checked="checked"';?&gt;&gt;&lt;/br&gt;&lt;/p&gt;
      &lt;input type="submit" value="submit"&gt; 
  
      &lt;/form&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>This is my table from which the respective editing field id is passed so we can view data in the form and can know which field of which data we want to edit ..</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  
  &lt;table border="2px"&gt;
  
  &lt;tr&gt;
  &lt;td&gt;ID&lt;/td&gt;
  &lt;td&gt;Title&lt;/td&gt;
  &lt;td&gt;Description&lt;/td&gt;
  &lt;td&gt;image&lt;/td&gt;
  &lt;td&gt;Action&lt;/td&gt;
  &lt;/tr&gt;
  &lt;?php foreach($query as $a):?&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;?php echo $a-&gt;id;?&gt;&lt;/td&gt;
  &lt;td&gt;&lt;?php echo $a-&gt;title;?&gt;&lt;/td&gt;
  &lt;td&gt;&lt;?php echo $a-&gt;description;?&gt;&lt;/td&gt;
  &lt;td&gt;&lt;img src="&lt;?php echo base_url();?&gt;/uploads/&lt;?php echo $a-&gt;image?&gt;" height="100px" width="100px"&gt;&lt;/td&gt;
  &lt;td&gt;&lt;?php echo anchor('blog_controller/add_post/'.$a-&gt;id,"Edit");?&gt;&lt;/td&gt;
  &lt;td&gt;&lt;?php echo anchor('blog_controler/delete_post/'.$a-&gt;id,"Delete");?&gt;&lt;/td&gt;
  
  &lt;?php endforeach;?&gt;
  &lt;/tr&gt;
  
  
  
  
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>