title: Can't update data in database with Codeigniter
body: <p>I can't update data in a record in CodeIgniter . Instead of updating it's adding new record in database table.'hotelres_id' is the primary key of my table.</p>
  
  <p>I have posted the code below:-</p>
  
  <p>Controller code for update:-</p>
  
  <pre><code>function edit($id){
      $this-&gt;load-&gt;model('hotel_reservation','mb');
      $data['message'] = '';
      $data['object'] = $this-&gt;mb-&gt;find_by_id($id);
  
      if($data['object']){
          $this-&gt;form_validation-&gt;set_rules('roomno', 'Room Number', 'required|is_unique[hotel_reservation.roomno]');
          $this-&gt;form_validation-&gt;set_rules('checkin', 'Check In', 'required|is_unique[hotel_reservation.checkin]');
          $this-&gt;form_validation-&gt;set_rules('checkout', 'Check Out', 'required|is_unique[hotel_reservation.checkout]');
  
          if ($this-&gt;form_validation-&gt;run() == TRUE){
              $this-&gt;mb-&gt;eventreg_id = $_POST['hotelres_id'];
              $this-&gt;mb-&gt;eventreg_id = $_POST['eventreg_id'];
              $this-&gt;mb-&gt;eventhotel_id = $_POST['eventhotel_id'];
              $this-&gt;mb-&gt;roomno = $_POST['roomno'];
              $this-&gt;mb-&gt;checkin = $_POST['checkin'];
              $this-&gt;mb-&gt;checkout = $_POST['checkout'];
              $this-&gt;mb-&gt;comment = $_POST['comment'];
              $this-&gt;mb-&gt;update();
              $data['message'] = 'Details updated successfully';
              $data['object'] = $this-&gt;mb;
          }
          $this-&gt;load-&gt;view('core/hotel_reservation/edit',$data);
      }
      else{
          $data['message'] = 'No details available!! Fill It!!';  
          $this-&gt;load-&gt;view('core/hotel_reservation/save',$data);
      }
  }
  </code></pre>
  
  <p>View Code :-</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
      &lt;title&gt;Hotel Reservation&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h2&gt;Hotel Reservation&lt;/h2&gt;
          &lt;?php if(isset($message)&amp;&amp;$message!='') echo "&lt;span class=\"message\"&gt;{$message}&lt;/span&gt;"; ?&gt;
          &lt;form action="&lt;?php echo site_url('core/hotel_re/edit/'.@$object-&gt;hotelres_id); ?&gt;" method="POST" &gt;
              &lt;table class="formtable"&gt;
  
              &lt;tr&gt;&lt;td&gt;hotelres_id&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="hotelres_id" id="hotelres_id"  class="textbox" value="&lt;?php echo @$object-&gt;hotelres_id; ?&gt;"  readonly&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;eventreg_id&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="eventreg_id" class="textbox" value="&lt;?php echo @$object-&gt;eventreg_id; ?&gt;"  readonly&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;eventhotel_id&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="eventhotel_id" class="textbox" value="&lt;?php echo @$object-&gt;eventhotel_id; ?&gt;" readonly&gt;&lt;/td&gt;&lt;/tr&gt;
  
  
              &lt;tr&gt;&lt;td&gt;Room Number&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="roomno"  value="&lt;?php echo @$object-&gt;roomno; ?&gt;"  class="textbox" &gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;Check In&lt;/td&gt;&lt;td&gt;&lt;input type="text"  name="checkin" value="&lt;?php echo @$object-&gt;checkin; ?&gt;"  class="textbox"&gt;&lt;/td&gt;&lt;/tr&gt;
  
              &lt;tr&gt;&lt;td&gt;Check Out&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="checkout" value="&lt;?php echo @$object-&gt;checkout; ?&gt;"  class="textbox"&gt;&lt;/td&gt;&lt;/tr&gt;
  
              &lt;tr&gt;&lt;td&gt;Comment&lt;/td&gt;&lt;td&gt;&lt;textarea type="text"  name="comment" value="&lt;?php echo @$object-&gt;comment; ?&gt;"  class="textarea" &gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;input type="submit" name="submit" value="Update" class="submitbutton"&gt;&lt;/td&gt;&lt;/tr&gt;
          &lt;/table&gt;
      &lt;/form&gt;
      &lt;span class="validation-errors"&gt;&lt;?php echo validation_errors(); ?&gt;&lt;/span&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Model code:-</p>
  
  <pre><code>&lt;?php
  class hotel_reservation extends CI_Model{
      var $hotelres_id;
      var $eventreg_id;
      var $eventhotel_id;
      var $roomno;
      var $checkin;
      var $checkout;
      var $comment;
  
      static $tablename = 'hotel_reservation';
      static $tableid = 'hotelres_id';
      function find_by_id($id)
      {
          $tableid = self::$tableid;
          $resultset = $this-&gt;db-&gt;get_where(self::$tablename,array($tableid=&gt;$id),1);
          if($resultset-&gt;num_rows()==1)
              return array_shift($resultset-&gt;result(get_class($this)));
          return false;
      }
  
      function find_all()
      {
          $resultset = $this-&gt;db-&gt;get(self::$tablename);
          return $resultset-&gt;result(get_class($this));
      }
  
      function save()
      {
          $tableid = self::$tableid;
          if(isset($this-&gt;$tableid)&amp;&amp;$this-&gt;$tableid!=''&amp;&amp;$this-&gt;$tableid!=0)
              $this-&gt;update();
          else
              $this-&gt;insert();
      }
  
      private function insert()
      {
          $this-&gt;db-&gt;insert(self::$tablename,$this);
      }
  
      function update()
      {
          $tableid = self::$tableid;
          $this-&gt;db-&gt;where($tableid,$this-&gt;$tableid);
          $this-&gt;db-&gt;update(self::$tablename,$this);
      }
  
      function delete()
      {
          $tableid = self::$tableid;
          $this-&gt;db-&gt;where($tableid,$this-&gt;$tableid);
          $this-&gt;db-&gt;delete(self::$tablename);
      }
  }
  </code></pre>