title: MySQL Update if value exists, Insert if not in PHP?
body: <pre><code>$sql_career = "REPLACE INTO career
           (id, battletag, lastHeroPlayed, lastUpdated, monsters, elites, hardcoreMonsters, barbarian, crusader, demonhunter, monk, witchdoctor, wizard, paragonLevel, paragonLevelHardcore)
           VALUES
           ('', '$battletag', '$lastHeroPlayed', '$lastUpdated', '$monsters', '$elites', '$hardcoreMonsters', '$barbarian', '$crusader', '$demonhunter', '$monk', '$witchdoctor', '$wizard', '$paragonLevel', '$paragonLevelHardcore')";
  </code></pre>
  
  <p><code>ID</code> auto increments.
  <code>battletag</code> is unique.</p>
  
  <p>Everything else changes over time. So I want to replace or update an entry if the <code>battletag</code> already exists without it making a new <code>id</code>. If it doesnt exist I want it to make a new entry letting the <code>id</code> auto increment for that unique <code>battletag</code>.</p>
  
  <hr>
  
  <p>This works with one problem: </p>
  
  <pre><code> $sql_career = "
      insert INTO career
        (id, battletag, lastHeroPlayed)
      VALUES
        (NULL, '$battletag', $lastHeroPlayed)
      on duplicate key
        update lastHeroPlayed=$lastHeroPlayed;
   ";
  </code></pre>
  
  <p>If I, for instance, load in two unique rows, the ID auto increments to 1 and then 2 for each. Then if I load up a row that has a duplicate of the unique key of one of the existing rows (and it then updates as it should) this actually triggers the auto increment. So if I then add in a third unique row, its number will be 4 instead of 3. </p>
  
  <p>How can I fix this?</p>