3
$this->db->beginTransaction();
$this->db->query ('LOCK TABLES users WRITE');
$sql = 'INSERT INTO users (uname) VALUES (:uname)';
$sth = $this->db->prepare ($sql);
$sth->bindParam (':uname', $uname);
$sth->execute ();
if ($sth->rowCount()==0) {
  $this->db->rollBack();
  $this->db->query ('UNLOCK TABLES');
  throw new Exception('<strong>Oh snap!</strong> User name is taken! Try again.');
}

我在我的数据库中手动设置了一个名为“test”的用户。当我创建一个名为“test2”的用户时,它就起作用了。但是每当我尝试创建第三个用户时,我都会得到rowCount = 0.

uname数据库中的varchar(15).

if(isset($_POST['regUser']) && isset($_POST['regPwd']) && isset($_POST['regConfirmPwd'])) {  
    if($_POST['regPwd'] == $_POST['regConfirmPwd'] ) {  
        $user->newUser($_POST['regUser'], $_POST['regPwd']);

} else {  
    $user->error = "<strong>Oh snap!</strong> The passwords don't match!";
  }  
}  

将发布信息发送到我的 newuser 函数,它会停在那里的第一位代码。有任何想法吗?

4

1 回答 1

1

与其插入用户然后回滚,不如测试该用户是否可用(在锁定表之后),然后放心地进行插入。

于 2013-02-27T13:36:09.297 回答