尝试在用户名存在时弹出错误

时间:2014-12-19 20:57:49

标签: php pdo

我在同一个问题上看过很多文章,并且无法将他们的答案转化为我的问题。基本上我在我的数据库中使我的字段“用户名”是唯一的,并希望在数据库中捕获现有用户名的错误,并回应其他用户友好的内容。截至目前,我能够收到错误消息,但无法控制它。  我只是想让用户知道数据库中是否已存在用户名。 任何帮助将不胜感激。

这是添加已使用“用户名”的成员时出现的错误。

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'theller' for key 'username'' in /Applications/MAMP/htdocs/TESTING/PHP/PHP-TEST/addMember.php:42 Stack trace: #0 /Applications/MAMP/htdocs/TESTING/PHP/PHP-TEST/addMember.php(42): PDOStatement->execute(Array) #1 {main} thrown in /Applications/MAMP/htdocs/TESTING/PHP/PHP-TEST/addMember.php on line 42

这是我的脚本,当我的ajax脚本执行并通过其数据发送时调用。

<?php ob_start();
          require_once 'classes/Config.php';
          try {
              $conn = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
              $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              $data = $conn->query('SELECT * FROM Testing');
          } catch(PDOException $e) {
                if(($PDO->errorCode() == 23000) || ($PDOStatement->errorCode() == 23000)) {
                  echo 'USER ALREADY EXIST';
                } 
                else {
                 // not a dupe key rethrow error
                 echo 'error';
                }
            }


        $form = $_POST;
        $firstname = htmlspecialchars($form[ 'firstname' ]);
        $lastname = htmlspecialchars($form[ 'lastname' ]);
        $username = htmlspecialchars($form[ 'username' ]);
        $password = htmlspecialchars($form[ 'password' ]);
        $passMatch = htmlspecialchars($form[ 'pass_match' ]);
        $message = htmlspecialchars($form[ 'message' ]);
        $email = htmlspecialchars($form[ 'email' ]);

        if (empty($firstname) || empty($lastname) || empty($username) || empty($password)|| empty($passMatch)|| empty($message)|| empty($email)){
            $error = "Complete all fields";
        }
        // Password match
        if ($password != $passMatch){
            $error = "Passwords don't match";
        }
        // Email validation
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $error = "Enter a  valid email";
        }
        if(IsInjected($email)) {
            $error = "Bad email value!";
        }
        if(!isset($error)){
            $sql = "INSERT INTO Testing ( firstname, lastname, message, email, username, password ) VALUES( :firstname, :lastname, :message, :email, :username, :password )";
            $query = $conn->prepare( $sql );
            $query->execute( array( ':firstname'=>$firstname, ':lastname'=>$lastname, ':message'=>$message, ':email'=>$email, ':username'=>$username, ':password'=>$password ));
            if(!$query->rowCount() > 0){
                echo "exists! cannot insert";
            }else{
            header('Location: thankyou.php'); 
            }

        }
        else{
            echo "error occured: ".$error;
            exit();
        }
?>

1 个答案:

答案 0 :(得分:1)

我最近的项目中遇到过类似的问题。请尝试将catch语句替换为以下内容:

catch(PDOException $e) {
    if(isset($conn))
    {
        if($conn->errorInfo()[0] == 23000) {
            echo 'USER ALREADY EXIST';
        } 
        else {
        // not a dupe key rethrow error
            echo 'error';
        }
    }
}

有关SQLSTATE错误的信息驻留在用于查询的PDO对象的errorInfo数组中