title: How can I use ON DUPLICATE KEY UPDATE in PDO with mysql?
body: <p><strong>DETAILS</strong></p>
  
  <p>I am doing a single insert for the expiry of a new or renewed licence. The time period for the expiry is 2 years from the insertion date.
  If a duplicate is detected, the entry will be updated such that <strong>the expiry equals the remaining expiry plus 2 years.</strong></p>
  
  <p>Regarding duplicates, in the example below there should only be one row containing user_id =55 and licence=commercial.</p>
  
  <p><strong>TABLE:</strong> licence_expiry</p>
  
  <pre><code>--------------------------------------------------------
  |   user_id   |   licence   |           expiry         |  
  --------------------------------------------------------
  |     55      |  commercial |     2013-07-04 05:13:48  |  
  ---------------------------------------------------------
  </code></pre>
  
  <p>user_id (int11), licence (varchan50), expiry (DATETIME)</p>
  
  <p>I think in mysql you would write it something like this (Please note that I haven't checked whether the code works in mysql. )</p>
  
  <pre><code>INSERT INTO `licence_expiry`
  (`user_id`, `licence`, `expiry`)
  VALUES
  (55, commercial, NOW()+ INTERVAL 2 YEAR)
  ON DUPLICATE KEY UPDATE
  `expiry` = `expiry` + INTERVAL 2 YEAR
  </code></pre>
  
  <p><strong>QUESTION:</strong> How can I do this with PDO? I've written a rough outline of what I think I will use, but I'm not sure what to write for the expiry value for the <strong>ON DUPLICATE KEY UPDATE.</strong></p>
  
  <pre><code>$sql = "INSERT INTO $table (user_id, licence, expiry)
                          VALUES (
                          :user_id,  
                          :licence,
                          :expiry)
      ON DUPLICATE KEY UPDATE expiry = Something"; 
  
  
  try {
      $dbh = new PDO('login info here');
      $dbh-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
      $stmt = $dbh-&gt;prepare($sql);
      $stmt-&gt;bindParam(':user_id', $userID, PDO::PARAM_INT);     
      $stmt-&gt;bindParam(':licence',$licence, PDO::PARAM_STR);
      $stmt-&gt;bindParam(':expiry',$expiry, PDO::PARAM_STR);                            
      $stmt-&gt;execute();
      //$stmt-&gt;closeCursor(); //use this instead of $dbh = null   if you will continue with another DB function
      $dbh = null; 
      }
  
      catch(PDOException $e)
      {
      $error=$e-&gt;getMessage();        
      }
  </code></pre>
  
  <p>Any help is much appreciated.</p>