title: Codeigniter : update records if records exists there or then insert new records
body: <p>In my Table i have four columns . </p>
  
  <ul>
  <li>r_id </li>
  <li>id (User id)</li>
  <li>v_id (company id)</li>
  <li>rate</li>
  </ul>
  
  <p>All i am doing is to rate the company (v_id) from user.</p>
  
  <p>Suppose, if user one is rate the 1st Company (v_id),Again when same user rate the same Company Then rate column automatically updated. if user one wants to rate another company then a new row will be added in the table. But in my case 
   when same user again rate the company then new row is inserted in the table.  </p>
  
  <p><strong>Model</strong></p>
  
  <p>I don't know where i am doing wrong in this Model.</p>
  
  <pre><code>function ratings($insert,$id,$update,$v_id) {
              // $this-&gt;db-&gt;where('id',$id);
              // $this-&gt;db-&gt;where('v_id',$v_id);
              // $run = $this-&gt;db-&gt;get('user_ratings');
              // if ($run-&gt;num_rows() &gt;= 1) {
              //  $this-&gt;db-&gt;where('id',$id);
             // $this-&gt;db-&gt;update('user_ratings',$update);
  
          // }else {
          //  // $this-&gt;db-&gt;set('user_id', $id);
           // $this-&gt;db-&gt;insert('user_ratings',$insert);
          // }
  
          $query = $this-&gt;db-&gt;query('select id, v_id from user_ratings where id = "'.$id.'" and v_id = "'.$v_id.'"')-&gt;num_rows();
  
          if ($query &gt; 0) {
              $this-&gt;db-&gt;query('update user_ratings set rate ="'.$update.'" where id = "'.$id.'"');
          }else {
              $this-&gt;db-&gt;insert('user_ratings',$insert);
          }
      } 
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>function ratings() {
              $id = $this-&gt;session-&gt;userdata('id');
              $v_id = $this-&gt;uri-&gt;segment(3);
              $insert= array (
                  'rate' =&gt; $this-&gt;input-&gt;post('click_val'),
                  'date' =&gt; date('Y-m-d H:i:s'),
                  'v_id' =&gt; $this-&gt;input-&gt;post('company_id'),
                  'id' =&gt; $this-&gt;input-&gt;post('id')
                  );
              $update = array (
                  'rate' =&gt; $this-&gt;input-&gt;post('click_val'),
                  'date' =&gt; date('Y-m-d H:i:s'),
  
                  );
              $this-&gt;Visa_mdl-&gt;ratings($value,$id,$update,$v_id);
          }
  </code></pre>