使用事务插入和更新相同的 table

Insert and update same table with transactions

因为我无法t/don不知道如何auto_increment 将两列合二为一table,所以我尝试通过事务来做到这一点。这就是我的尝试

$pdo->beginTransaction();
try 
{
       $sql = "INSERT INTO users ( username, password, firstname, lastname, email, user_image, path)
                    VALUES (:username, :password, :firstname, :lastname, :email, :user_image, :path)";

       $q = $pdo->prepare($sql);
       $q->execute(array(
                    ':username' => $username,
                    ':password' => sha1($password),
                    ':firstname'    => $firstname,
                    ':lastname' => $lastname,
                    ':email'    => $email,
                    ':user_image'   => $forDB,
                    ':path'     => $path,
                    ));                     

       $lastInsertID = $pdo->lastInsertId();
       $sql = $pdo->prepare("INSERT INTO users (usertype) 
                            VALUE (:user_id)");
       $sql->execute(array(
                    ':user_id'      => $lastInsertID
                    ));
       $pdo->commit();      
            }               
                // any errors from the above database queries will be catched
            catch (PDOException $e)
            {
                    // roll back transaction
                    $pdo->rollback();
                    // log any errors to file
                    ExceptionErrorHandler($e);
                exit;
            }

所以基本上我想在列 usertype 中插入此记录的 ID (user_id) 两列必须相等。 现在,当我尝试使用此 .. 它保存空字段,但 usertype 已更新为 lastInsertID

改变

$sql = $pdo->prepare("INSERT INTO users (usertype) 
                    VALUE (:user_id)");

至此

$sql = $pdo->prepare("UPDATE users SET usertype=:user_id WHERE user_id=:user_id");