PDO和PHP事务未插入数据且没有错误

时间:2016-03-20 17:38:36

标签: php mysql pdo

我有oho和pdo代码在两个单独的sql表上执行一个事务,代码在下面提供。

try{    
    echo " we begin transaction ";
    $dbh->beginTransaction();   
    $stmt2 = $dbh->prepare( "INSERT INTO `members`( `username`, `email`, `password`, `time_created`) 
                  VALUES (:username , :email , :password , NOW())");

    $stmt2->bindParam(':username', $username , PDO::PARAM_STR);
    $stmt2->bindParam(':email', $email , PDO::PARAM_STR);
    $stmt2->bindParam(':password', $password , PDO::PARAM_STR); 

    $stmt3 = $dbh->prepare("INSERT INTO `members_groups` (`member_id` , `group_id`) 
                           VALUES((SELECT MAX(member_id) FROM members) , 
                               (SELECT group_id FROM groups WHERE permission = 0 ))");

    if($stmt2->execute() && $stmt3->execute()){
        $dbh->commit(); 
        echo "registered successfully";             
    }
}
catch (Exception $e){
    $dbh->rollback();
    echo "an error has occurred".$e.getMessage();
}   

但是代码被执行,数据没有插入表中,也没有产生错误。 但所有参数都已设定。

0 个答案:

没有答案