如何防止重复的电子邮件被提交

时间:2020-01-17 18:56:57

标签: php pdo

如果正在提交的电子邮件已经存在,我正在寻找一种以我的形式返回消息的方法。我要第四个被执行

    $conn = new PDO("mysql:host=localhost;dbname=ali","root","");
    $sql = "insert into admin(id,email,password,repeatpass) values(null,?,?,?)";
    $result = $conn->prepare($sql);
    $result ->bindValue(1,$_POST["email"]);
    $result ->bindValue(2,$_POST["pass"]);
    $result ->bindValue(3,$_POST["repeat"]);
    $number = $result->rowCount();



    if (empty($_POST["email"]) or empty($_POST["pass"]) or empty($_POST["repeat"])){
        echo "fill all the blanks";
    }
    else if(strlen($_POST["pass"]) < 6){
        echo "password should not be less than 6 characters!";
    }
    else if($_POST["pass"] !== $_POST["repeat"]){
        echo "passwords not matched";
    }
    else if($number > 0){
        echo "is already exist";
    }
    else{

        $result->execute();
        echo "successfuly registered";
    }

1 个答案:

答案 0 :(得分:0)

您需要捕获受影响的行并决定是否要发送消息。

让我们修改一下您的代码:

    $query = $result->execute();
    if ($query == 1) {                    // 1 - one row affected means inserted
        echo "successfuly registered";
    } else {                              // 0 - no row affected means no insert
        echo "Sorry - No Insert done. User exists / failure in database.";  // this replaces your code above
    }

之间:您的代码行

    $number = $result->rowCount();

来得早。到目前为止,您尚未执行查询。 因此,您可以删除以下内容:

else if($number > 0){
    echo "is already exist";
}