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

时间:2016-02-10 14:52:37

标签: php mysql if-statement user-registration

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

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

不好的部分是if语句的其他部分(例如else)将不起作用。我遇到的两个问题是if语句的最后一个问题只是让我直接进入" 500 Sever Error"页面,无论我在表格上输入什么 - 当下"注册"点击我直接到这个页面。在评论if语句的最后一个部分,只是检查我要显示的消息,显示一旦输入密码不匹配,只给我一个空白页 - 没有错误消息。

我已经注释了诸如$ 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>

我真的很感激这方面的一些帮助。非常感谢。

1 个答案:

答案 0 :(得分:2)

您的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;