我的 if 语句进行检查但没有输出

My if statement does the checks but gives no output

所以我有 3 个 if 语句来检查用户名和密码是否不为空,执行我的语句并检查我的注册表单上的两个密码是否匹配,即 $password 和 $confirmpassword。

好的部分是它允许检查密码文本框中输入的密码是否与确认密码文本框中重复的密码匹配,如果匹配则用户成功注册并被定向到之后的登录页面,我可以使用散列密码在我的数据库中看到此人的详细信息。

不好的部分是 if 语句的其他部分(例如 else)将不起作用。我遇到的两个问题是 if 语句的最后一个 else 直接将我带到“500 服务器错误”页面,无论我在表单上输入什么 - 单击 "register" 的那一刻我直接进入这一页。在评论 if 语句的最后 else 部分并只是检查我要显示的消息时,一旦输入不匹配的密码就会显示,只会给我一个空白页 - 没有错误消息。

我已经注释掉诸如 $message 变量之类的东西,并用 echo 替换它,只是为了确定这些东西是否搞砸了,只是为了在空白屏幕上看到某种消息,但什么也没有。

我知道我的 if 语句中至少有两个有效,但感觉好像我的内部 if 语句不起作用:

if ($stmt->execute()):
    echo 'Well done! You have successfully registered with us!';
  else:
    echo 'There seems to be an issue getting you registered.';

这是执行这些检查的代码 - register.php:

<?php

ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(-1);

require 'connection.php';

$message = '';

if (!empty($_POST['username']) && !empty($_POST['password'])):

  // $username = $_POST['username'];
  $password = $_POST['password'];
  $confirmpassword = $_POST['confirmpassword'];

//var_dump($password, $confirmpassword);

  if($password == $confirmpassword):

  $sql = "INSERT INTO users (FirstName, LastName, Role, Email, Username, Password) VALUES (:firstname, :lastname, :role, :email, :username, :password)";
  $stmt = $conn->prepare($sql);
  $hashPassword = password_hash($password, PASSWORD_DEFAULT);

  $stmt->bindParam(':firstname', $_POST['firstname']);
  $stmt->bindParam(':lastname', $_POST['lastname']);
  $stmt->bindParam(':role', $_POST['role']);
  $stmt->bindParam(':email', $_POST['email']);
  $stmt->bindParam(':username', $_POST['username']);
  $stmt->bindParam(':password', $hashPassword);

  if ($stmt->execute()):
    echo 'Well done! You have successfully registered with us!';
    //$message = 'Well done! You have successfully registered with us!';
    //header('Location:loginPage.php');
  else:
    echo 'There seems to be an issue getting you registered.';
    //$message = 'There seems to be an issue getting you registered.';
  //else:
    //echo 'Your passwords do not match!';
    //$message = 'Your passwords do not match, please enter them correctly.';

endif;

endif;

endif;

这是我的 HTML - registerPage.php:

--><form class="col-1-3" action="register.php" method="post">

<fieldset class="register-group">

  <label>
    <br>First Name*
    <input type="text" name="firstname" placeholder="FirstName" value="<?php echo $firstname; ?>">
  </label>

  <label>
    Last Name*
    <input type="text" name="lastname" placeholder="LastName" value="<?php echo $lastname; ?>">
  </label>

  <label>
    Role*
    <select name="role">
      <option value="Student" selected>Student</option>
      <option value="Tutor" selected>Tutor</option>
      <option value="Admin" selected>Admin</option>
    </select>
  </label>

<label>
  Email Address*
  <input type="email" name="email" placeholder="Email"  value="<?php echo $email; ?>">
</label>

<label>
  Choose a Username*
  <input type="text" name="username" placeholder="Username" value="<?php echo $username; ?>">
</label>

<label>
  Choose a Password*
  <input type="password" name="password" placeholder="Password">
</label>

<label>
  Repeat Password*
  <input type="password" name="confirmpassword" placeholder="Confirm Password">
</label>

  <input class="btn btn-default" type="submit" name="register" value="Register">

</form>

非常感谢您对此提供的帮助。非常感谢。

您的 if 结构不匹配。您需要将最后一个 else 移到第一个 endif 之后。所以喜欢:

if (!empty($_POST['username']) && !empty($_POST['password'])):
    //...
    if($password == $confirmpassword):
        //...
        if ($stmt->execute()):
            echo 'Well done! You have successfully registered with us!';
            //...
        else:
            echo 'There seems to be an issue getting you registered.';
            //...
        endif;
    else:
        echo 'Your passwords do not match!';
        //...
    endif;
endif;