title: SQL IF EXIST UPDATE ELSE INSERT prepared statement
body: <p>I'm breaking my brains over this, i would realy appriciate help!</p>
  
  <p>This is the code i have so far.. </p>
  
  <pre><code>$conn = db_connect();                       
  $sql = "INSERT INTO measurements
  (`date`, `weight`, `waist`, `id`) VALUES (?,?,?,?)";
  $stmt = $conn-&gt; prepare($sql); 
  $stmt -&gt;bind_param("sddi", $date, $_POST['weight'], $_POST['waist'], $user_id);
  $stmt-&gt;execute();
  $stmt-&gt;close();
  $conn-&gt;close();
  </code></pre>
  
  <p>Its a prepared statement for an sql insert. Now i want to change it to a IF EXIST THEN UPDATE ELSE insert the way i am doing right now. something like this but then with a prepared statement:</p>
  
  <pre><code>IF EXISTS 
  (SELECT * FROM measurements WHERE user_id=’4’)
  UPDATE measurements SET (`weight`=40, `waist`=45) WHERE user_id=’4’
  ELSE
  INSERT INTO measurements 
  VALUES (`date`='week 1', `weight`= 40, `waist`=45, `id`=4)
  </code></pre>
  
  <p>I found some articles on stackoverflow about the if EXIST then update else insert but i did not find it with a prepared statement in it that worked for me.</p>
  
  <p>Thanks a thousand!</p>
  
  <p>UPDATE:</p>
  
  <p>i've changed it to dublicate key style. </p>
  
  <pre><code>$sql = "
  INSERT INTO measurements (uniqueID, date, weight, waist) 
  VALUES ('$uniqueID', '$date', '$weight',  '$waist') 
  ON DUPLICATE KEY UPDATE weight= '$weight', waist= '$waist'";
  
  $conn-&gt;query($sql);
  </code></pre>
  
  <p>Now the second part of the question, how do i make this a prepared statement?</p>