title: insert-update pdo mysql query and get last inserted id, if needed
body: <p>I have simple insert/update page where I do check if there is "id", if no then proceed insert query otherwise proceed update query,  and this works fine. Problem is that I need that id for the next query, in case of Insert query I will get it as "lastInsertId" but in case of Update query "lastInsertId" will overwrite $_POST["id"]. </p>
  
  <p>Is there a way to identify which query has been performed (insert or update) and then get right id, or I have to bind/ execute query's separately?  </p>
  
  <pre><code>$id= $_POST["id"];  
    try { 
      $DBH = db_connect ();
      $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        if ($id== "") {
           $query= $DBH-&gt;prepare('INSERT INTO table (x1,x2) VALUES(:x1,:x2)');                        
        } else {
           $query= $DBH-&gt;prepare('UPDATE day SET x1=:x1, x2=:x2 WHERE id= :id'); 
             $query-&gt; bindValue(':id', $id);
        }
  
             $query-&gt; bindValue(':x1', $x1);
             $query-&gt; bindValue(':x2', $x2);                                                                  
             $query-&gt; execute();                              
  
          $id= $DBH-&gt; lastInsertId('id') ; //get last inserted ID
      } 
  </code></pre>