PHP - 如果密码不匹配,则无法显示输出文本

PHP - can't get to show output text if passwords doesn't match

为此苦苦挣扎了这么久... 故事:我试图将 "password confirm" ($repassword) 添加到我的注册表中。还添加了一个字符串,上面写着“密码不匹配”,如果是这种情况(如果密码不相同),它应该被激活。
问题:一切正常,除了如果密码不同它不会显示该消息。
如果您不着急的话,再讲一个故事:我已经尝试了几种方法。例如,将其放入函数 (function valid_pws) 或将其放入单个 if 语句中。其中一些通过输出“密码不匹配”文本来工作,但是如果用户单击 "Register" 按钮,它总是显示该消息。哦,数据库一切都很好。它总是理解我想做什么,只是不能显示(如果需要)或隐藏(如果不需要)该文本。

PHP代码:

<?php
require ('insert.php'); 

function valid_pws($password,$repassword) {

     // Validate pws.
     if (empty($password || $repassword)) {
         return false;
     } 
     else if ($password !== $repassword) {
         // error matching passwords
         return false;
     }
     else {
     return true; // passwords match
     }
}

// create that user
function create_user($name, $username, $password, $email) {
        require ('insert.php');
        $query = "INSERT INTO `users` (Name, Username, Password, EMail) VALUES('$name','$username', '$password', '$email')";
        $result = mysqli_query($connection, $query);

      if($result){
          return true;  // Success
      }else{
          return false; // Error somewhere
      }
}

// If the values are posted, insert them into the database.
    if (isset($_POST['username']) && isset($_POST['password'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $repassword = $_POST['repassword'];

        // I'm not using hash atm
        $hash = password_hash($password, PASSWORD_DEFAULT);

        //valid_pws function in a var
        $pwerr = valid_pws($password,$repassword);

        //if there's everything fine with passwords then good
        if ($pwerr === true){
        //if there's everything fine with creating user then good
        if (create_user($name, $username, $password, $email)) {
              $smsg = "User registration success.";
        }
        else{
            //if there's something wrong with pws, then error. Should get it from function if false is returned... or not?
            if($pwerr === false){
                $rmsg = "Passwords doesn't match";
            }
            else {
            // if there's something else with registration, then error
          $fmsg = "User registration failed.";
            }
        }
    }
}

mysqli_close($connection);
?>

HTML代码:

<form action="" method="POST" class="form-horizontal" role="form">

        <!-- How registering went -->
        <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
        <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
        <?php if(isset($rmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $rmsg; ?> </div><?php } ?>

        <!-- Registration form begins -->
        <h2>Register</h2><br>

            <label for="Name">Name</label>
            <input name="name" type="text" id="name" maxlength="40" placeholder="Name" class="form-control" required autofocus> <!-- end -->

            <label for="email">Email</label>
            <input name="email" type="email" id="email" maxlength="65" placeholder="Email" class="form-control"> <!-- end -->


            <label for="userName">User</label>
            <input name="username" type="text" id="userName" maxlength="12" placeholder="Username" class="form-control" required> <!-- end -->

            <label for="password">Password</label>
            <input name="password" type="password" id="password" maxlength="50" placeholder="Password" class="form-control" required> <!-- end -->

            <label for="password">Repassword</label>
            <input name="repassword" type="password" maxlength="50" placeholder="Password again" class="form-control"> <!-- end -->


            <button type="submit" class="btn btn-primary btn-block">Register</button>
        </form>

你把条件搞砸了应该是:

    if ($pwerr === true){
            //if there's everything fine with creating user then good
            if (create_user($name, $username, $password, $email)) {
                  $smsg = "User registration success.";
            }
      }//<-------- note that coditopn over here
            else{
                //if there's something wrong with pws, then error. Should get it from function if false is returned... or not?
                if($pwerr === false){
                    $rmsg = "Passwords doesn't match";
                }
                else {
                // if there's something else with registration, then error
              $fmsg = "User registration failed.";
                }
            }

尝试改变这个条件:

if ($pwerr === true){
        if (create_user($name, $username, $password, $email)) {
            $smsg = "User registration success.";
        }else{
            $fmsg = "User registration failed.";
        }
    }else{
        $rmsg = "Passwords doesn't match";
    }

您的代码存在问题,如果密码不匹配,它永远不会进入您验证的地方。

您可以这样做:

<?php
    function valid_pws($password,$repassword) {
        // Validate pws.
        if (empty($password || $repassword)) {
            return false;
        } else if ($password !== $repassword) {
            // error matching passwords
            return false;
        }
        return true; // passwords match
    }
    //valid_pws function in a var
    $pwerr = valid_pws("teste","tete");

    //if there's everything fine with passwords then good
    if ($pwerr === true){
        //if there's everything fine with creating user then good
        if (create_user($name, $username, $password, $email)) {
            $smsg = "User registration success.";
        } else {
            // if there's something else with registration, then error
            $fmsg = "User registration failed.";
        }
    } else {
        $rmsg = "Passwords doesn't match";
    }

    if(isset($smsg)){ echo $smsg; }
    if(isset($fmsg)){ echo $fmsg; }
    if(isset($rmsg)){ echo $rmsg; }

上面的代码将打印 Passwords doesn't match