title: PHP / Mysql / Highscore table - How to check existing scores and replace them
body: <p>I have a simple game with a highscore system. The user can write his name and his score will be saved in the highscore table. The problem is: When a user plays several time with the same name there's always a new entry into the database. How can I change my code that there will be only one entry per name. How can I check the usernames existing score and only update the score when it's better? How to do this in my code? Thanks in advance</p>
  
  <pre><code>&lt;?php
  header('Access-Control-Allow-Origin: *');
  
  require 'config.php';
  
  mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  mysql_select_db("$db_name")or die("cannot select DB");
  
  if(isset($_GET['name']) &amp;&amp; isset($_GET['score'])) {
  
   //Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks
   $name = strip_tags(mysql_real_escape_string($_GET['name']));
   $score = strip_tags(mysql_real_escape_string($_GET['score']));
   $sql = mysql_query("INSERT INTO `$tbl_name` (`id`,`name`,`score`) VALUES ('','$name','$score');");
  
   if($sql){
  
        //The query returned true - now do whatever you like here.
        echo 'Your score was saved. Congrats!';
  
   }else{
  
        //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
        echo 'There was a problem saving your score. Please try again later.'.mysql_error();;
  
   }
  
  }else{
   echo 'Your name or score wasnt passed in the request. Make sure you add ?name=NAME_HERE&amp;score=1337 to the tags.';
  }
  
  mysql_close($dblink);//Close off the MySQL connection to save resources.
  ?&gt;
  </code></pre>