'密码确认密码'错误

时间:2014-09-17 18:16:42

标签: php mysql post passwords

我在这里有我的代码。我只想在我的网站上添加确认密码文本字段。但是,如果我放if($_POST['password'] != $_POST['confirm_password']){ $message = "Your passwords did not match."; }。它只是显示密码不匹配但仍然,它将进入我的数据库。我想要的是如果密码和确认密码文本字段不匹配,将出现一个对话框告诉错误。除非完整正确,否则不会将其放入数据库中。

这是我的PHP代码。

    <?php

if(isset($_POST["register"])){

if(!empty($_POST['full_name']) && !empty($_POST['email']) && !empty($_POST['username']) && !empty($_POST['password'])) {
    $full_name=$_POST['full_name'];
    $email=$_POST['email'];
    $username=$_POST['username'];
    $password=$_POST['password'];
    $confirm_password = $_POST['confirm_password'];


    $query=mysql_query("SELECT * FROM usertbl WHERE username='".$username."'");
    $studnoquery=mysql_query("");





    $numrows=mysql_num_rows($query);

    $passconfirm=($_POST['password'] != $_POST['confirm_password']);

    if($numrows==0)

    {


    $sql="INSERT INTO usertbl
            (full_name, email, username,password) 
            VALUES('$full_name','$email', '$username', '$password')";

    $result=mysql_query($sql);

    if($result){
     $message = "Account Successfully Created";
    } else {
     $message = "Failed to insert data information!";
    }
    } 

    //if numrows is greater than 1
    else {
     $message = "That username already exists! Please try another one!";
    }
} 


else {
     $message = "All fields are required!";
}
}
?>

我认为if($_POST['password'] != $_POST['confirm_password'])是正确的,但我很难将其放在我的代码上。谢谢。帮助我。

1 个答案:

答案 0 :(得分:0)

这是更新的代码,只需替换它。放置$error变量。如果密码和确认密码不匹配,那么$error变量的值将为1,否则它将为空。

  <?php

if(isset($_POST["register"])){

if(!empty($_POST['full_name']) && !empty($_POST['email']) && !empty($_POST['username']) && !empty($_POST['password'])) {
    $full_name=$_POST['full_name'];
    $email=$_POST['email'];
    $username=$_POST['username'];
    $password=$_POST['password'];
    $confirm_password = $_POST['confirm_password'];


    $query=mysql_query("SELECT * FROM usertbl WHERE username='".$username."'");
    $studnoquery=mysql_query("");





    $numrows=mysql_num_rows($query);
    $error = '';
    if($_POST['password'] != $_POST['confirm_password']) { 
            $message = "Your passwords did not match."; 
            $error = 1;
    }
    $passconfirm=($_POST['password'] != $_POST['confirm_password']);

    if($numrows==0 && $error=='')

    {


    $sql="INSERT INTO usertbl
            (full_name, email, username,password) 
            VALUES('$full_name','$email', '$username', '$password')";

    $result=mysql_query($sql);

    if($result){
     $message = "Account Successfully Created";
    } else {
     $message = "Failed to insert data information!";
    }
    } 

    //if numrows is greater than 1
    else {
     $message = "That username already exists! Please try another one!";
    }
} 


else {
     $message = "All fields are required!";
}
}
?>