如果存在,PHP检查用户

时间:2017-10-25 08:52:56

标签: php forms

我正在创建一个注册表单,我想检查非空的输入字段,还要检查用户的电子邮件是否存在以阻止表单提交。由于某种原因,我的代码无法正常工作。我收到用户存在的错误,但是当我检查我的数据库时,添加了用户。我的代码有问题。希望有人可以提供帮助

由于

完整代码:

<form role="form" method="post" action="">

<?php 
    if(isset($_POST['register'], $_POST['user_firstname'], $_POST['user_lastname'], $_POST['user_email'], $_POST['user_username'], $_POST['user_password'], $_POST['user_dob'])) {

        $firstname = mysqli_real_escape_string($connection, $_POST['user_firstname']);
        $lastname = mysqli_real_escape_string($connection, $_POST['user_lastname']);
        $email = mysqli_real_escape_string($connection, $_POST['user_email']);
        $username = mysqli_real_escape_string($connection, $_POST['user_username']);
        $password = mysqli_real_escape_string($connection, $_POST['user_password']);
        $dob = mysqli_real_escape_string($connection, $_POST['user_dob']);
        $registration_date = date('y-m-d');

        $check_if_user_exist = "SELECT user_email FROM users WHERE user_email = '$email' ";
        $check_if_user_exist_connection = mysqli_query($connection, $check_if_user_exist);

        if(mysqli_num_rows($check_if_user_exist_connection) >= 1) {
            echo "User Exist";
        }

        if(!empty($firstname) && !empty($lastname) && !empty($email) && !empty($username) && !empty($password) && !empty($dob)) {

        $registration_form_query = "INSERT INTO users (user_firstname, user_lastname, user_email, user_username, user_password, user_dob, user_registration_date) ";
        $registration_form_query .= "VALUES ('$firstname', '$lastname', '$email', '$username', '$password', '$dob',  now()) ";

        $registration_form_query_connection = mysqli_query($connection, $registration_form_query);

        if(!$registration_form_query_connection) {
            die("Error" .mysqli_error($connection));
        }

        echo "<p class='bg-success'>Thank You For Register</p>";

    }else {
            echo "<p class='bg-danger'>Please Fill In All The Fields</p>";
        }

    }

?>

<div class="form-group">
  <label for="user_firstname">First Name</label>
   <input type="text" name="user_firstname" class="form-control">
</div>

<div class="form-group">
  <label for="user_lastname">Last Name</label>
   <input type="text" name="user_lastname" class="form-control">
</div>

<div class="form-group">
  <label for="user_email">Email</label>
   <input type="email" name="user_email" class="form-control">
</div>

<div class="form-group">
  <label for="user_username">Username</label>
   <input type="text" name="user_username" class="form-control">
</div>

<div class="form-group">
  <label for="user_password">Password</label>
   <input type="password" name="user_password" class="form-control">
</div>

<div class="form-group">
  <label for="user_dob">Date of Birth</label>
   <input type="date" name="user_dob" class="form-control">
</div>

<button type="submit" name="register" class="btn btn-primary">Send</button>
</form>

2 个答案:

答案 0 :(得分:1)

就是这种情况,因为你只回显字符串&#34; User Exist&#34;如果您找到了用户,则实际上并未阻止脚本运行。您必须使用相应的else语句来阻止插入:

if(mysqli_num_rows($check_if_user_exist_connection) >= 1) {
        echo "User Exist";
}
else {   

    if(!empty($firstname) && !empty($lastname) && !empty($email) && !empty($username) && !empty($password) && !empty($dob)) {

        $registration_form_query = "INSERT INTO users (user_firstname, user_lastname, user_email, user_username, user_password, user_dob, user_registration_date) ";
        $registration_form_query .= "VALUES ('$firstname', '$lastname', '$email', '$username', '$password', '$dob',  now()) ";
        $registration_form_query_connection = mysqli_query($connection, $registration_form_query);

    if(!$registration_form_query_connection) {
        die("Error" .mysqli_error($connection));
    }

    echo "<p class='bg-success'>Thank You For Register</p>";
}

答案 1 :(得分:0)

这很简单。

if($check_if_user_exist_connection->num_rows >= 1) 
{
  //User Exist
}
else 
{
  //User not Exist
}