douchi1945 2015-07-09 19:31
浏览 121
已采纳

用户注册数据库问题

So, this is a script to dump the user info into the database using the PDO driver. Whenever I run the script with a unique username it works and it dumps the info in. But, if a user with the same username tries to sign up it should throw an error. I'm using $stmt->errorCode() == 23000 to check for duplicates in the database but whenever I try and register a username I've already registered I just get a blank white screen. I've ran through the script with xDebug and it looks like when I try and sign up with a username that's already in the database it's not even hitting the if/else statements at the bottom of the script. It's just executing and throwing a blank white screen.

EDIT: I do have the username field in the DB set to 'unique'.

Video of xDebug walkthrough: http://screencast.com/t/znZfMgV2

Any ideas?

// create a salt using the current timestamp
$salt = time();

// encrypt the password and salt
$password = sha1($password.$salt);

// prepare the sql statement
$sql = 'INSERT INTO users (created, username, salt, password) VALUES(now(), :username, :salt, :password)';
$stmt = $conn->prepare($sql);
// bind the parameters
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->bindParam(':salt', $salt, PDO::PARAM_INT);
$stmt->bindParam(':password', $password, PDO::PARAM_STR);
$stmt->execute();

if ($stmt->rowCount() == 1) {
    $registerResult = "$username has been registered. You may now log in.";
} elseif ($stmt->errorCode() == 23000) {
    $registerResult = "$username is already in use. Please choose another username.";
} else {
    $registerResult = 'Sorry, there was a problem with the database';
}
  • 写回答

2条回答 默认 最新

  • dongzhan5943 2015-07-09 19:44
    关注

    Checking for errorcodes will not work, because the duplicate error will throw an exception on the call to execute().

    You need to catch PDOException :

    try {
        // create a salt using the current timestamp
        $salt = time();
    
        // encrypt the password and salt
        $password = sha1($password.$salt);
    
        // prepare the sql statement
        $sql = 'INSERT INTO users (created, username, salt, password) VALUES(now(), :username, :salt, :password)';
        $stmt = $conn->prepare($sql);
        // bind the parameters
        $stmt->bindParam(':username', $username, PDO::PARAM_STR);
        $stmt->bindParam(':salt', $salt, PDO::PARAM_INT);
        $stmt->bindParam(':password', $password, PDO::PARAM_STR);
        $stmt->execute();
    
        if ($stmt->rowCount() == 1) {
            $registerResult = "$username has been registered. You may now log in.";
        }
    } catch (PDOException $e) {
        if ($stmt->errorCode() == 23000) {
            $registerResult = "$username is already in use. Please choose another username.";
        } else {
            $registerResult = 'Sorry, there was a problem with the database';
        }
        die($e->getMessage());
    }
    

    Make sure your connection is set up to throw exception of course:

    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?