我一直在尝试创建一个注册表单,我已经尝试了几种方法,但最后我添加的是else if($ status =" OK")行。以下是我的代码的一部分,请帮助我仍然是编码新手;
<?php
$surnameErr = $firstnameErr = $usernameErr = $emailErr = $passwordErr = $password1Err = "";
$surname = $firstname = $username = $email = $password = $password1 = "";
$status = "OK";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["surname"])) {
$surnameErr = "Surname is required";
$status = "NOTOK";
}
else {
$surname = test_input($_POST["surname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
$surnameErr = "Only letters and white space allowed";
$status = "NOTOK";
}
}
if (empty($_POST["firstname"])) {
$firstnameErr = "First Name is required";
$status = "NOTOK";
}
else {
$firstname = test_input($_POST["firstname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
$firstnameErr = "Only letters and white space allowed";
$status = "NOTOK";
}
}
if (empty($_POST["username"])) {
$usernameErr = "Username is required";
$status = "NOTOK";
} else {
$username = test_input($_POST["username"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
$status = "NOTOK";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid Email format";
$status = "NOTOK";
}
}
if (empty($_POST["password"])) {
$passwordErr = "Password is required";
$status = "NOTOK";
}
if (empty($_POST["password1"])) {
$password1Err = "Repeat password to confirm";
$status = "NOTOK";
}
if ($password != $password1){
echo "<script>alert('Password does not match')</script>";
$status = "NOTOK";
}
$connection = mysqli_connect("localhost", "root", "", "church") or die(mysqli_error());
$checkuser = mysqli_query ($connection,"SELECT * FROM signup WHERE username = '$username'");
if (mysqli_num_rows ($checkuser) > 0){
echo "<script>alert('$username already exist in our database, please enter another one')</script>";
$status = "NOTOK";
}
$checkemail = mysqli_query ($connection,"SELECT * FROM signup WHERE email = '$email'");
if (mysqli_num_rows ($checkemail) > 0){
echo "<script>alert('$email already exist in our database, please enter another one')</script>";
$status = "NOTOK";
}
else if ($status = "OK"){
$password=md5($password);
$datainsert = mysqli_query ($connection, "INSERT INTO signup (surname, firstname, username, email, password) VALUES ('$surname', '$firstname','$username','$email','$password')") or die(mysqli_error());
echo "<script>alert('Sign Up Successful!')</script>";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
答案 0 :(得分:1)
else if ($status = "OK"){
此行表示“将$ status设置为”OK“的值并返回该值”。除非您将$ status设置为PHP认为为空的值(0,false,NULL,空字符串等),否则这将始终为true。
需要else if ($status == "OK"){
才能正常工作。
另外,如果在测试的左侧放置常量或函数/方法调用,则可以避免此类错误。由于在正常执行期间无法为函数/方法调用或常量赋值,因此代码将失败并出现致命错误,因此调试非常容易。
else if ("OK" = $status){ // Will fatal with a line number. Fixable in 2 seconds flat