PDO事务无声地失败

时间:2014-07-06 13:46:48

标签: php mysql pdo

我的代码:

try
            {
                $this->pdo->beginTransaction();

                $create_user = $this->pdo->prepare('INSERT INTO users
                                                                (user_pass, user_email, display_name, user_activation_key, user_status)
                                                         VALUES (:user_pass, :user_email, :display_name, :user_activation_key, :user_status)');
                $execute = $create_user->execute(array(
                    ':user_pass'           => $password['first'],
                    ':user_email'          => $email,
                    ':display_name'        => $name,
                    ':user_activation_key' => $this->guidv4(),
                    ':user_status'         => 0
                ));

                if ($execute)
                {
                    $user_id = $this->pdo->lastInsertId();
                    $create_meta = $this->pdo->prepare('INSERT INTO users_meta
                                                                    (user_id, meta_key, meta_value)
                                                             VALUES (:user_id, :meta_key, :meta_value)');
                    if ($meta)
                    {
                        $count_meta = 0;
                        foreach ($meta as $key => $value)
                        {
                            if ($key == 'tel'
                                OR $key == 'tos_agree'
                                /* Add other meta options here */)
                            {
                                $create_meta->bindValue(':user_id', (int) trim($user_id), PDO::PARAM_INT);
                                $create_meta->bindValue(':meta_key', trim($key), PDO::PARAM_STR);
                                $create_meta->bindValue(':meta_value', trim($value), PDO::PARAM_STR);
                                $execute = $create_meta->execute();
                                $count_meta++;
                            }
                        }
                        if ($count_meta == 2 /* Increase this number if you add more meta options */ )
                        {
                            return true;
                        }
                    }
                    else
                    {
                        return true;
                    }
                }

                $this->pdo->commit();
            }
            catch (PDOException $e)
            {
                echo $e->getMessage();
            }

问题:当我添加pdo->beginTransaction()commit()时,数据库中没有插入任何数据。如果我注释掉这些行数据是否正确插入。

我没有收到任何错误。

我想确保meta和user表都是原子更新的。

1 个答案:

答案 0 :(得分:0)

事务失败,因为代码中的return true;语句未执行提交:

                   if ($count_meta == 2 /* Increase this number if you add more meta options */ )
                    {
                        return true;
                    }
                }
                else
                {
                    return true;
                }
            }
            // this line would not be in the code path
            $this->pdo->commit();

结论:不要在事务中使用代码路径的缩写。

相关问题