Fumler
Fumler

Reputation: 485

rowCount returns 0 even if it should not

$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.');
}

I set up a user in my database manually that is called "test". And when I created a user called "test2" it worked. But whenever I try to create a third user I get rowCount = 0.

The uname in the db is 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!";
  }  
}  

Send the post info to my newuser function and it stops on the first bit of code there. Any ideas?

Upvotes: 3

Views: 238

Answers (1)

acutesoftware
acutesoftware

Reputation: 1091

Instead of inserting the user and then rolling back if taken, it might be better to test if that user is available (after locking the table), and then doing the insert with confidence.

Upvotes: 1

Related Questions