注册总是给我默认错误

时间:2014-02-07 21:04:22

标签: php pdo

我正在进行登录和注册系统,使用PDO进行数据库连接。当我想检查用户名时,它返回默认错误:

Sorry, that username is already taken. Please choose another one.

即使我只是输入hduigifugfulgfrfg,它总是给我这个错误:/

我的代码:

 } else if ($this->databaseConnection()) {
        $query_check_user_name = $this->db_connection->prepare('SELECT user_name, user_email FROM users WHERE user_name=:user_name OR user_email=:user_email');
        $query_check_user_name->bindValue(':user_name', $user_name, PDO::PARAM_STR);
        $query_check_user_name->bindValue(':user_email', $user_email, PDO::PARAM_STR);
        $query_check_user_name->execute();
        $result = $query_check_user_name->fetchAll();
        if (count($result) > 0) {
            for ($i = 0; $i < count($result); $i++) {
                $this->errors[] = ($result[$i]['user_name'] == $user_name) ? MESSAGE_USERNAME_EXISTS : MESSAGE_EMAIL_ALREADY_EXISTS;
            }

有没有人看到错误或其他什么?我正在寻找一个我不再看到的东西了:P

感谢。

1 个答案:

答案 0 :(得分:0)

以下声明的原因是什么?

for ($i = 0; $i < count($result); $i++) {
    //Do Something
}

尝试用以下代码替换代码:

 } else if ($this->databaseConnection()) {
    $query_check_user_name = $this->db_connection->prepare('SELECT user_name, user_email FROM users WHERE user_name=:user_name OR user_email=:user_email');
    $query_check_user_name->bindValue(':user_name', $user_name, PDO::PARAM_STR);
    $query_check_user_name->bindValue(':user_email', $user_email, PDO::PARAM_STR);
    $query_check_user_name->execute();
    $result = $query_check_user_name->fetchAll();
    if (count($result) > 0) {
       $this->errors[] = ($result[$i]['user_name'] == $user_name) ? MESSAGE_USERNAME_EXISTS : MESSAGE_EMAIL_ALREADY_EXISTS;
        }

其余的代码看起来非常好,它说的是有多少行具有相同的用户名或电子邮件。如果它超过大于0则输出错误。除for语句外,这似乎完全没问题。