Insert and update in one query in mysql codeigniter
<p>I am new on codeigniter,I am doing the following things but I didn't get the solution.</p>

<p>Here is the following things one by one.</p>

<ol>
<li><p>First I fetch the data from database and display in table format by foreach loop.</p>

<pre><code>&lt;?php
</code></pre>

<p>$attributes = array('class' => 'form-horizontal','id'=>'update_form');
echo form_open('emp_hodm_update/update_hodm', $attributes);
?></p>




  date
  Work
  Partner
  Director
  Time
  Task
  Status
  Action




    
    
      date;?>" class="" />
    
    
      work;?>" class="" />
    
    
      partner;?>" class="" />
    
    
      director;?>" class="" />
    
    
      time;?>" class="" />
    
    
      task;?>
    
    
      status;?>
    
    
      Delete
    
    


 

<p>
</p>

 </li>
<li><p>By clicking add button it creates new rows SO I want to update the old data as well as new row data which is created by add buttond to insert into a table.</p></li>
<li><p>But I didn't get any idea how to do this. </p></li>
</ol>

<p>Controller:</p>

<pre><code>public function update_hodm(){

/* Checking the all validation of task form*/
//$this-&gt;form_validation-&gt;set_rules('date', 'Date', 'required');
$this-&gt;form_validation-&gt;set_rules('work[]', 'Types of Work', 'required');
//$this-&gt;form_validation-&gt;set_rules('partner[]', 'Worked With', 'required');
$this-&gt;form_validation-&gt;set_rules('director[]', 'Director', 'required');
$this-&gt;form_validation-&gt;set_rules('time[]', 'No Of Hours', 'required');
$this-&gt;form_validation-&gt;set_rules('task[]', 'Task Details', 'required');
$this-&gt;form_validation-&gt;set_rules('status[]', 'Task Status', 'required');

if ($this-&gt;form_validation-&gt;run()) {
    /* Taking the data from form*/
    $todayDate = date('Y-m-d');
    $work=$this-&gt;input-&gt;post('work');
    $partner=$this-&gt;input-&gt;post('partner');
    $director=$this-&gt;input-&gt;post('director');
    $time=$this-&gt;input-&gt;post('time');
    $task=$this-&gt;input-&gt;post('task');
    $status=$this-&gt;input-&gt;post('status');

    $count=count($this-&gt;input-&gt;post('work'));
    $data =array();
    for($i=0; $i&lt;$count; $i++) {
    $data[$i] = array(
        'name'       =&gt;$this-&gt;session-&gt;userdata('emp_name'),
        'date'       =&gt;$todayDate,
        'work'       =&gt;$work[$i],
        'partner'    =&gt;$partner[$i],
        'director'   =&gt;$director[$i],
        'time'       =&gt;$time[$i],
        'task'       =&gt;$task[$i],
        'status'     =&gt;$status[$i]
        );
        }
        $add=$this-&gt;update-&gt;update_hodm($data,$todayDate);
        /* Display Success message if data updated successfully in database*/

            if($add){
                $this-&gt;session-&gt;set_flashdata('hodm_form',"All HODM Data Inserted Successfully.");
                $this-&gt;session-&gt;set_flashdata('hodm_form_class','alert-success');
            }else{
                /* Displaying the error message*/
                $this-&gt;session-&gt;set_flashdata('hodm_form',"failed to add, Please Try again");
                $this-&gt;session-&gt;set_flashdata('hodm_form_class','alert-danger');
            }
        return redirect('home');
} else {
    $this-&gt;load-&gt;view('public/digital_hodm_view');
}
}
</code></pre>

<p>Model:</p>

<pre><code>public function update_hodm($data,$todayDate){
        $this-&gt;db-&gt;where('date',$todaydate);
        $this-&gt;db-&gt;update_batch('task_form', $data,'date');//date is my table column name 
        return true;
    }
</code></pre>

<ol start="4">
<li>Please help me to find the solution.</li>
</ol>

<p>Thanks in advance</p>