title: How to prevent duplicate entry of user id and also rating field when both are already exist in a row?
body: <p>I am facing a problem in PHP star rating when the user logged in and given a rating in a product, the data is entered into the table, in that time I also update the ratings, but the problem is when I logged out and then again enter using my session id, the data row is not updated,rather it will the product rating again with user id but the product field is empty at the same time in the database. Included jQuery and PHP code:</p>
  
  <pre><code>$(function(){
      $('.rating').barrating({
          theme: 'fontawesome-stars',
          onSelect: function(value, text, event){
              // Get element id by data-id attribute
              var el = this;
              var el_id = el.$elem.data('id');
              // rating was selected by a user
              if(typeof(event)!=='undefined'){
  
                  var split_id  = el_id.split("_");
  
                  var productId = split_id[1];  // productId
                  // AJAX Request
                  $.ajax({
                       url: 'rating_ajaxData.php',
                      type: 'post',
                      data: {productId:productId,rating:value},
                      dataType: 'json',
                      success: function(data){
                          // Update average
                          var average=data['averageRating'];
                          $('#avgrating_'+productId).text(average);
                      }
                  });
              }
          }
      });
  });
  </code></pre>
  
  <p><code>rating_ajaxData.php</code></p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  require 'lib/Session.php';
  Session::init();
  require 'lib/Database.php';
  require 'classes/Customer.php';
  $cmr =  new Customer();
  $db  =  new Database();
  $cmrId =  Session::get("customerId"); 
  if(isset($_POST['productId']) &amp;&amp; isset($_POST['rating'])){
    $productId = $_POST['productId'];
    $rating    = $_POST['rating'];
    //$ratingProcess = $cmr-&gt;processRatingByUser($productid,$rating);  
  
          $query     = "SELECT COUNT(*) AS cntproduct FROM tbl_rating WHERE cmrId = '$cmrId' AND productId = '$productId'";
          $result    =  $db-&gt;select($query)-&gt;fetch_assoc();                   
              $count = $result['cntproduct'];       
              if($count==0){
              $insertquery  = "INSERT INTO tbl_rating(cmrId,productId,rating) VALUES('$cmrId','$productId','$rating')";
              $ratingInsert = $db-&gt;insert($insertquery);
              //return $ratingInsert;            
              }else{
                  $updtquery    = "UPDATE tbl_rating
                                   SET    
                                   rating       = '$rating' 
                                   WHERE cmrId  = '$cmrId' AND productId = '$productId'";
                  $ratingupdate = $db-&gt;update($updtquery);
                  //return $ratingupdate;
              }  
  
    //get average
    $avgratingquery = "SELECT ROUND(AVG(rating),1) as averageRating FROM tbl_rating WHERE productId = '$productId'";
    $avgresult = $db-&gt;select($avgratingquery)-&gt;fetch_assoc();
    $averageRating = $avgresult['averageRating'];
  
    $return_arr = array("averageRating"=&gt;$averageRating);
  
    echo json_encode($return_arr);  
  }                    
  ?&gt;
  </code></pre>