簡體   English   中英

簡單的PHP注冊表單/函數中的錯誤

[英]Error in simple PHP registration form / function

我有以下PHP函數,當他們通過表單發布電子郵件地址時,嘗試使用臨時密碼在數據庫中注冊用戶:

 public function registerNewUser() {

        $temp_pass = '';
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $random_string_length=8;
        for ($i = 0; $i < $random_string_length; $i++) {
            $temp_pass .= $characters[rand(0, strlen($characters) - 1)];
        }

            if (empty($_POST['user_email'])) {

                $this->errors[] = FEEDBACK_EMAIL_FIELD_EMPTY;

            } elseif (strlen($_POST['user_email']) > 64) {

                $this->errors[] = FEEDBACK_EMAIL_TOO_LONG;

            } elseif (!filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) {     

                $this->errors[] = FEEDBACK_EMAIL_DOES_NOT_FIT_PATTERN;

} elseif (!empty($_POST['user_email'])
  && strlen($_POST['user_email']) <= 64
  && filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) {

    $this->user_email = htmlentities($_POST['user_email'], ENT_QUOTES);

    $this->user_password = $temp_pass;

    $this->hash_cost_factor = (defined('HASH_COST_FACTOR') ? HASH_COST_FACTOR : null);
    $this->user_password_hash = password_hash($this->user_password, PASSWORD_DEFAULT, array('cost' => $this->hash_cost_factor));

    $sth = $this->db->prepare("SELECT * FROM users WHERE user_email = :user_email ;");
    $sth->execute(array(':user_email' => $this->user_email));

    $count =  $sth->rowCount();            

    if ($count == 1) {

        $this->errors[] = FEEDBACK_USEREMAIL_ALREADY_TAKEN;

    } else {

        $this->user_activation_hash = sha1(uniqid(mt_rand(), true));

        $sth = $this->db->prepare("INSERT INTO users (user_email, user_password_hash, user_activation_hash) VALUES(:user_email, :user_password_hash, :user_activation_hash) ;");
        $sth->execute(array(':user_email' => $this->user_email, ':user_password_hash' => $this->user_password_hash, ':user_activation_hash' => $this->user_activation_hash));                    

        $count =  $sth->rowCount();

        if ($count == 1) {

            $this->user_id = $this->db->lastInsertId();                      

                        // send a verification email
            if ($this->sendVerificationEmail()) {

                            // when mail has been send successfully
                $this->messages[] = FEEDBACK_ACCOUNT_SUCCESSFULLY_CREATED;
                $this->registration_successful = true;
                return true;

            } else {

                $sth = $this->db->prepare("DELETE FROM users WHERE user_id = :last_inserted_id ;");
                $sth->execute(array(':last_inserted_id' => $this->db->lastInsertId() ));                            

                $this->errors[] = FEEDBACK_VERIFICATION_MAIL_SENDING_FAILED;

            }

        } else {

            $this->errors[] = FEEDBACK_ACCOUNT_CREATION_FAILED;

        }
    }            

} else {

    $this->errors[] = FEEDBACK_UNKNOWN_ERROR;

}          

        // standard return. returns only true of really successful (see above)
return false;
}

我不斷跳出FEEDBACK_ACCOUNT_CREATION_FAILED錯誤,但不知道原因。 有任何想法嗎?

插入后,您是否已丟棄“ $ sth”? 那給你什么? 如果您使用的是mysql,則可以打開general_log( http://dev.mysql.com/doc/refman/5.1/en/query-log.html )以查看執行的mysql查詢。 這樣,您可以查看查詢是否正確創建。

如果不確定另一端發生了什么,打開mysql日志記錄可能會非常有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM