Php事务:PHP PDO事务

我有一个注册页面,基本上我需要将数据插入到 4 个表中。

基本上,如果任何插入失败,我不希望任何添加到数据库中,这似乎很简单。

我的困惑是,我需要先插入用户的用户名,电子邮件,密码等在我的users表,所以我可以得到(不知道如何)使用 PDO 的 UID MySQL 已经给了我的用户(由 mysql 自动递增)。

但同时我想确保,如果例如在users表中插入数据(所以我可以得到 uid MySQL 给用户),如果任何其他插入失败,它将删除插入到users表中的数据。

我认为最好展示我的代码;我已经注释掉了代码,并在代码中进行了解释,这可能会使它更容易理解。

// Begin our transaction, we need to insert data into 4 tables:
// users, users_status, users_roles, users_profiles
// connect to database
$dbh = sql_con();
// begin transaction
$dbh->beginTransaction();
try {
    // this query inserts data into the `users` table
    $stmt = $dbh->prepare('
                        INSERT INTO `users`
                        (users_status, user_login, user_p, user_email, user_registered)
                        VALUES
                        (?, ?, ?, ?, NOW())');
    $stmt->bindParam(1, $userstatus,     PDO::PARAM_STR);
    $stmt->bindParam(2, $username,       PDO::PARAM_STR);
    $stmt->bindParam(3, $HashedPword, PDO::PARAM_STR);
    $stmt->bindParam(4, $email,          PDO::PARAM_STR);
    $stmt->execute();
    // get user_uid from insert for use in other tables below
    $lastInsertID = $dbh->lastInsertId();
    // this query inserts data into the `users_status` table
    $stmt = $dbh->prepare('
                        INSERT INTO `users_status`
                        (user_uid, user_activation_key)
                        VALUES
                        (?, ?)');
    $stmt->bindParam(1, $lastInsertID,     PDO::PARAM_STR);
    $stmt->bindParam(2, $activationkey,    PDO::PARAM_STR);
    $stmt->execute();
    // this query inserts data into the `users_roles` table
    $stmt = $dbh->prepare('
                        INSERT INTO `users_roles`
                        (user_uid, user_role)
                        VALUES
                        (?, ?)');
    $stmt->bindParam(1, $lastInsertID,      PDO::PARAM_STR);
    $stmt->bindParam(2, SUBSCRIBER_ROLE,    PDO::PARAM_STR);
    $stmt->execute();
    // this query inserts data into the `users_profiles` table
    $stmt = $dbh->prepare('
                        INSERT INTO `users_profiles`
                        (user_uid)
                        VALUES
                        (?)');
    $stmt->bindParam(1, $lastInsertID,      PDO::PARAM_STR);
    $stmt->execute();
    // commit transaction
    $dbh->commit();
} // any errors from the above database queries will be catched
catch (PDOException $e) {
    // roll back transaction
    $dbh->rollback();
    // log any errors to file
    ExceptionErrorHandler($e);
    require_once($footer_inc);
    exit;
}

我是 PDO 的新手,上面可能有错误或问题,我还没有注意到,因为我不能测试,直到我弄清楚我的问题。

我需要知道如何在用户表中插入用户数据,这样我就可以得到 uid MySQL 给我的用户

然后得到 uid,因为我需要它的其他表

但同时,如果查询在插入用户表后由于任何原因失败,则数据也将从用户表中删除。

19

此函数返回刚刚插入的记录的主键:PDO::lastInsertId您将需要它用于NEED_USERS_UID_FOR_HERE参数。在 INSERT 语句之后使用它。

由于您启动了事务,如果发生任何错误,数据将不会插入任何表中,只要您使用 InnoDB 引擎为您的 MySQL 表(MyISAM 不支持事务)。

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(724)
软件工程开发项目实例:软件工程术语(engineering terminology)
上一篇
Confuse ing:ingsrc中的PHP变量
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(13条)