所以在我将新的if
语句添加到我的代码中检查字符串长度之前,它工作得很好。我现在尝试添加一个额外的if
语句,以查看表单上的密码长度是否满足6到32个字符长的条件,但由于某种原因,我仍然收到错误(500服务器错误)
我试过一遍又一遍地阅读代码,但似乎无法发现是什么让我这个错误。我不确定这是否是我放置此声明可能导致这种情况或更小和更简单的事情。
会对此提出一些建议。提前谢谢。
<?php
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(-1);
require 'connection.php';
$message = '';
$username = $_POST['username'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
if (!empty($_POST['username']) && !empty($_POST['password'])):
//var_dump($password, $confirmpassword);
if(strlen($password) < 6 || strlen($password) > 32):
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!';
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.';
endif;
else:
echo 'Your password must be between 6 to 32 characters.';
else:
echo 'Your passwords do not match, please enter them correctly.';
header('Location:registerPage.php');
endif;
endif;
endif;
答案 0 :(得分:1)
快速浏览可能的错误原因:
else
语句,后面是第30行的另一个else
。header(Location:)
,因为header()
无法在其之前输出/打印任何内容,因此无法正常工作。此外,通常内部错误(500)是由Apache的错误配置而不是代码错误引起的。您是否尝试删除额外的if()
语句并查看代码是否有效?
使用代码编辑:
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(-1);
require 'connection.php';
$message = '';
$username = $_POST['username'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
if (!empty($_POST['username']) && !empty($_POST['password'])):
//var_dump($password, $confirmpassword);
if(strlen($password) < 6 || strlen($password) > 32):
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!';
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.';
endif;
else:
echo 'Your password must be between 6 to 32 characters.';
endif;
// ========= You need to add `endif` (above) before starting another `else`
else:
echo 'Your passwords do not match, please enter them correctly.';
endif;
header('Location:registerPage.php');
endif;
编辑2,相同的代码,但附加可视块行(警告:将产生语法错误):
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(-1);
require 'connection.php';
$message = '';
$username = $_POST['username'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
if (!empty($_POST['username']) && !empty($_POST['password'])):
//var_dump($password, $confirmpassword);
if(strlen($password) < 6 || strlen($password) > 32):
|
| 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!';
| | 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.';
| | endif;
| else:
| echo 'Your password must be between 6 to 32 characters.';
| endif;
|
// ========= You need to add `endif` (above) before starting another `else`
else:
echo 'Your passwords do not match, please enter them correctly.';
endif;
header('Location:registerPage.php');
endif;