title: SQL - IF EXISTS UPDATE ELSE INSERT INTO
body: <p>What I'm trying to do is <code>INSERT</code> subscribers in my database, but <code>IF EXISTS</code> it should <code>UPDATE</code> the row, <code>ELSE INSERT INTO</code> a new row.</p>
  
  <p>Ofcourse I connect to the database first and <code>GET</code> the <code>$name</code>, <code>$email</code> and <code>$birthday</code> from the url string.</p>
  
  <pre><code>$con=mysqli_connect("localhost","---","---","---");
  // Check connection
  if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
  
  $name=$_GET['name']; 
  $email=$_GET['email'];
  $birthday=$_GET['birthday'];
  </code></pre>
  
  <p>This works, but just adds the new row;</p>
  
  <pre><code>mysqli_query($con,"INSERT INTO subs (subs_name, subs_email, subs_birthday)
  VALUES ('$name', '$email', '$birthday')");
  
  mysqli_close($con);
  </code></pre>
  
  <p>Here's what I tried;</p>
  
  <pre><code>mysqli_query($con,"INSERT INTO subs (subs_name, subs_email, subs_birthday)
  VALUES '$name', '$email', '$birthday'
  ON DUPLICATE KEY UPDATE subs_name = VALUES($name), subs_birthday = VALUES($birthday)");
  mysqli_close($con);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>mysqli_query($con,"IF EXISTS (SELECT * FROM subs WHERE subs_email='$email')
      UPDATE subs SET subs_name='$name', subs_birthday='$birthday' WHERE subs_email='$email'
  ELSE
      INSERT INTO subs (subs_name, subs_email, subs_birthday) VALUES ('$name', '$email', '$birthday')");
  mysqli_close($con);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>mysqli_query($con,"IF NOT EXISTS(SELECT * FROM subs WHERE subs_email='$email')
  Begin
  INSERT INTO subs (subs_name, subs_email, subs_birthday)
  VALUES ('$name', '$email', '$birthday')
  End");
  mysqli_close($con);
  </code></pre>
  
  <p>But none of them work, what am I doing wrong?</p>
  
  <p>Any help is greatly appreciated!</p>