title: PHP CODEIGNITER UPDATE issue
body: <p>I can't update data in a record in CodeIgniter . </p>
  
  <p>I have posted the code below:-</p>
  
  <p><strong>//CONTROLLER
  //RESERVATION.php</strong></p>
  
  <pre><code>&lt;?php
  defined('BASEPATH') OR exit('No direct script access allowed');
  
  class Reservation extends CI_Controller {
  
      public function __construct()
      {
          parent::__construct();
          $this-&gt;load-&gt;model('reservation_model','reservation');
      }
  
      public function index()
      {
          $this-&gt;load-&gt;helper('url');
          $this-&gt;load-&gt;view('manage_reservation');
      }
      public function reservationview()
      {
          $this-&gt;load-&gt;helper('url');
          $this-&gt;load-&gt;view('view_reservation');
      }
  
      public function ajax_list()
      {
          $list = $this-&gt;reservation-&gt;get_datatables();
          $data = array();
          $no = $_POST['start'];
          foreach ($list as $reservation) {
              $no++;
              $row = array();
              $row[] = $reservation-&gt;id;
              $row[] = $reservation-&gt;dateReserved;
              $row[] = $reservation-&gt;requestedBy;
              $row[] = $reservation-&gt;facility;
              $row[] = $reservation-&gt;dateFrom;
              $row[] = $reservation-&gt;dateTo;
              $row[] = $reservation-&gt;timeStart;
              $row[] = $reservation-&gt;timeEnd;
              $row[] = $reservation-&gt;status;
              $row[] = $reservation-&gt;items;
              $row[] = $reservation-&gt;purpose;
  
              //add html for action
              $row[] = '&lt;a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit_reservation('."'".$reservation-&gt;id."'".')"&gt;&lt;i class="glyphicon glyphicon-pencil"&gt;&lt;/i&gt;&lt;/a&gt;
                    &lt;a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_reservation('."'".$reservation-&gt;id."'".')"&gt;&lt;i class="glyphicon glyphicon-trash"&gt;&lt;/i&gt;&lt;/a&gt;';
  
              $data[] = $row;
          }
  
          $output = array(
                          "draw" =&gt; $_POST['draw'],
                          "recordsTotal" =&gt; $this-&gt;reservation-&gt;count_all(),
                          "recordsFiltered" =&gt; $this-&gt;reservation-&gt;count_filtered(),
                          "data" =&gt; $data,
                  );
          //output to json format
          echo json_encode($output);
      }
  
      public function ajax_edit($id)
      {
  
          $data = $this-&gt;reservation-&gt;get_by_id($id);
  
          $data-&gt;dateFrom = ($data-&gt;dateFrom == '0000-00-00') ? '' : $data-&gt;dateFrom;
          $data-&gt;dateTo = ($data-&gt;dateTo == '0000-00-00') ? '' : $data-&gt;dateTo;       // if 0000-00-00 set tu empty for datepicker compatibility
          echo json_encode($data);
  
      }
  
      public function ajax_add()
      {
          $this-&gt;_validate();
          $data = array(
                  'id' =&gt; $this-&gt;input-&gt;post('id'),
                  'dateReserved' =&gt; $this-&gt;input-&gt;post('dateReserved'),
                  'requestedBy' =&gt; $this-&gt;input-&gt;post('requestedBy'),
                  'facility' =&gt; $this-&gt;input-&gt;post('facility'),
                  'dateFrom' =&gt; $this-&gt;input-&gt;post('dateFrom'),
                  'dateTo' =&gt; $this-&gt;input-&gt;post('dateTo'),
                  'timeStart' =&gt; $this-&gt;input-&gt;post('timeStart'),
                  'timeEnd' =&gt; $this-&gt;input-&gt;post('timeEnd'),
                  'status' =&gt; $this-&gt;input-&gt;post('status'),
                  'items' =&gt; $this-&gt;input-&gt;post('items'),
                  'purpose' =&gt; $this-&gt;input-&gt;post('purpose'),
  
              );
          $insert = $this-&gt;reservation-&gt;save($data);
          echo json_encode(array("status" =&gt; TRUE));
      }
  
      public function ajax_update()
      {
          $this-&gt;_validate();
          $data = array(
  
                  'id' =&gt; $this-&gt;input-&gt;post('id'),
                  'dateReserved' =&gt; $this-&gt;input-&gt;post('dateReserved'),
                  'requestedBy' =&gt; $this-&gt;input-&gt;post('requestedBy'),
                  'facility' =&gt; $this-&gt;input-&gt;post('facility'),
                  'dateFrom' =&gt; $this-&gt;input-&gt;post('dateFrom'),
                  'dateTo' =&gt; $this-&gt;input-&gt;post('dateTo'),
                  'timeStart' =&gt; $this-&gt;input-&gt;post('timeStart'),
                  'timeEnd' =&gt; $this-&gt;input-&gt;post('timeEnd'),
                  'status' =&gt; $this-&gt;input-&gt;post('status'),
                  'items' =&gt; $this-&gt;input-&gt;post('items'),
                  'purpose' =&gt; $this-&gt;input-&gt;post('purpose'),
  
  
  
              );
          $this-&gt;reservation-&gt;update(array('id' =&gt; $this-&gt;input-&gt;post('id')), $data);
          echo json_encode(array("status" =&gt; TRUE));
      }
  
      public function ajax_delete($id)
      {
          $this-&gt;reservation-&gt;delete_by_id($id);
          echo json_encode(array("status" =&gt; TRUE));
      }
  
  
      private function _validate()
      {
          $data = array();
          $data['error_string'] = array();
          $data['inputerror'] = array();
          $data['status'] = TRUE;
  
          if($this-&gt;input-&gt;post('requestedBy') == '')
          {
              $data['inputerror'][] = 'requestedBy';
              $data['error_string'][] = 'Requested Name is required*';
              $data['status'] = FALSE;
          }
  
          if($this-&gt;input-&gt;post('dateFrom') == '')
          {
              $data['inputerror'][] = 'dateFrom';
              $data['error_string'][] = 'Please Select a Date*';
              $data['status'] = FALSE;
          }
  
          if($this-&gt;input-&gt;post('dateTo') == '')
          {
              $data['inputerror'][] = 'dateTo';
              $data['error_string'][] = 'Please Select a Date*';
              $data['status'] = FALSE;
          }
  
          if($this-&gt;input-&gt;post('timeStart') == '')
          {
              $data['inputerror'][] = 'timeStart';
              $data['error_string'][] = 'Please select a Time*';
              $data['status'] = FALSE;
          }
  
          if($this-&gt;input-&gt;post('timeEnd') == '')
          {
              $data['inputerror'][] = 'timeEnd';
              $data['error_string'][] = 'Please select a Time*';
              $data['status'] = FALSE;
          }
  
          if($this-&gt;input-&gt;post('status') == '')
          {
              $data['inputerror'][] = 'status';
              $data['error_string'][] = 'Please Indicate Status*';
              $data['status'] = FALSE;
          }
  
          if($this-&gt;input-&gt;post('items') == '')
          {
              $data['inputerror'][] = 'items';
              $data['error_string'][] = 'Please select an Item*';
              $data['status'] = FALSE;
          }
  
          if($this-&gt;input-&gt;post('purpose') == '')
          {
              $data['inputerror'][] = 'purpose';
              $data['error_string'][] = 'Please indicate a Purpose*';
              $data['status'] = FALSE;
          }
  
          if($data['status'] === FALSE)
          {
              echo json_encode($data);
              exit();
          }
      }
  
  }
  </code></pre>
  
  <p><strong>//MODEL
  //Reservation_model.php</strong></p>
  
  <pre><code>    &lt;?php
      defined('BASEPATH') OR exit('No direct script access allowed');
  
      class Reservation_model extends CI_Model {
  
          var $table = 'tblreservation';
          var $column_order = array('id','dateReserved','requestedBy','facility','dateFrom','dateTo','timeStart','timeEnd','status','items','purpose',null); //set column field database for datatable orderable
          var $column_search = array('id','dateReserved','requestedBy','facility','dateFrom','dateTo','timeStart','timeEnd','status','items','purpose'); //set column field database for datatable searchable just firstname , lastname , address are searchable
  
          var $order = array('id' =&gt; 'desc'); // default order 
  
          public function __construct()
          {
              parent::__construct();
              $this-&gt;load-&gt;database();
          }
  
          private function _get_datatables_query()
          {
  
              $this-&gt;db-&gt;from($this-&gt;table);
  
              $i = 0;
  
              foreach ($this-&gt;column_search as $item) // loop column 
              {
                  if($_POST['search']['value']) // if datatable send POST for search
                  {
  
                      if($i===0) // first loop
                      {
                          $this-&gt;db-&gt;group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
                          $this-&gt;db-&gt;like($item, $_POST['search']['value']);
                      }
                      else
                      {
                          $this-&gt;db-&gt;or_like($item, $_POST['search']['value']);
                      }
  
                      if(count($this-&gt;column_search) - 1 == $i) //last loop
                          $this-&gt;db-&gt;group_end(); //close bracket
                  }
                  $i++;
              }
  
              if(isset($_POST['order'])) // here order processing
              {
                  $this-&gt;db-&gt;order_by($this-&gt;column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
              } 
              else if(isset($this-&gt;order))
              {
                  $order = $this-&gt;order;
                  $this-&gt;db-&gt;order_by(key($order), $order[key($order)]);
              }
          }
  
          function get_datatables()
          {
              $this-&gt;_get_datatables_query();
              if($_POST['length'] != -1)
              $this-&gt;db-&gt;limit($_POST['length'], $_POST['start']);
              $query = $this-&gt;db-&gt;get();
              return $query-&gt;result();
          }
  
          function count_filtered()
          {
              $this-&gt;_get_datatables_query();
              $query = $this-&gt;db-&gt;get();
              return $query-&gt;num_rows();
          }
  
          public function count_all()
          {
              $this-&gt;db-&gt;from($this-&gt;table);
              return $this-&gt;db-&gt;count_all_results();
          }
  
          public function get_by_id($id)
          {
              $this-&gt;db-&gt;from($this-&gt;table);
              $this-&gt;db-&gt;where('id',$id);
              $query = $this-&gt;db-&gt;get();
  
              return $query-&gt;row();
          }
  
          public function save($data)
          {
              $this-&gt;db-&gt;insert($this-&gt;table, $data);
              return $this-&gt;db-&gt;insert_id();
          }
  
          public function update($where, $data)
          {
              $this-&gt;db-&gt;update($this-&gt;table, $data, $where);
              return $this-&gt;db-&gt;affected_rows();
          }
  
          public function delete_by_id($id)
          {
              $this-&gt;db-&gt;where('id', $id);
              $this-&gt;db-&gt;delete($this-&gt;table);
          }
  
  
  }
  </code></pre>
  
  <p><strong>VIEW =  manage_reservation.php = save function</strong></p>
  
  <pre><code>function save()
  {
      $('#btnSave').text('saving...'); //change button text
      $('#btnSave').attr('disabled',true); //set button disable 
      var url;
  
      if(save_method == 'add') {
          url = "&lt;?php echo site_url('reservation/ajax_add')?&gt;";
      } else {
          url = "&lt;?php echo site_url('reservation/ajax_update')?&gt;";
      }
  
      // ajax adding data to database
      $.ajax({
          url : url,
          type: "POST",
          data: $('#form').serialize(),
          dataType: "JSON",
          success: function(data)
          {
  
              if(data.status) //if success close modal and reload ajax table
              {
                  $('#modal_form').modal('hide');
                  reload_table();
              }
              else
              {
                  for (var i = 0; i &lt; data.inputerror.length; i++) 
                  {
                      $('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
                      $('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
                  }
              }
              $('#btnSave').text('save'); //change button text
              $('#btnSave').attr('disabled',false); //set button enable 
  
  
          },
          error: function (jqXHR, textStatus, errorThrown)
          {
              alert('Error adding / update data');
              $('#btnSave').text('save'); //change button text
              $('#btnSave').attr('disabled',false); //set button enable 
  
          }
      });
  }
  </code></pre>
  
  <p><strong>NEWBIE TO CODE IGNITER any help will be appreciated :)</strong></p>