title: Execute update in the same time
body: <p>I have two tables - users and codes. I need to insert user and allocate for created account one first free (not alocated - id_user=0) row with code (prize).</p>
  
  <p>Below I attach my code. Do you think thats enough to ensure that there will not be situation where one code will be allocated to more than one user ? </p>
  
  <p>I mean if one row will be taken in the same time and in result only one user will take the prize ?</p>
  
  <pre><code>     try {
                  $this-&gt;pdo-&gt;beginTransaction();
  
                  $result = $this-&gt;pdo-&gt;prepare(
                          "INSERT INTO `users`(`id`, `fn`, `sn`, `email`, `city`, `cdatet`, `ldatet`, `active`, `hash`) VALUES (0, :fn, :sn, :email, :city, now(), now(), 0, :hash)");
                  $result-&gt;bindParam(':fn', $fn);
                  $result-&gt;bindParam(':sn', $sn);
                  $result-&gt;bindParam(':email', $mail);
                  $result-&gt;bindParam(':city', $city);
                  $result-&gt;bindParam(':hash', $hash);
                  $result-&gt;execute();
  
                  $lastId = $this-&gt;pdo-&gt;lastInsertId();
  
                  $result2 = $this-&gt;pdo-&gt;prepare(
                          "UPDATE `codes` SET `id_user`=:uid WHERE `id_user`=0 ORDER BY `class` DESC LIMIT 1");
                  $result2-&gt;bindParam(':uid', $lastId);
                  $result2-&gt;execute();
  
                  $this-&gt;pdo-&gt;commit();
                  return $lastId;
      } catch (PDOException $e)  {
                  $this-&gt;logError($e-&gt;getCode(), $e-&gt;getMessage());
                  $this-&gt;pdo-&gt;rollBack();
      }     
      return 0;
  </code></pre>