我正在学习如何在php中创建注册页面的教程,但当我在注册部分键入结果时,我总是收到一个空的输入错误。我想这是因为我得到了一个错误,我的$result变量是未定义的。我反复看了很多次教程,发现有错别字,但最终还是累坏了自己。
这是我的.signup.inc.php文件
<?php
// no ending tag when only php code
if (isset($_POST["submit"])) {
//$ signifies a variable
$name = $_POST["name"];
$email = $_POST["email"];
$username = $_POST["uid"];
$pwd = $_POST["pwd"];
$pwdrepeat = $_POST["pwdrepeat"];
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
if(emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) !== false) {
header("location: ../signup.php?error=emptyinput");
exit();
}
if(invalidUid($username) !== false) {
header("location: ../signup.php?error=invaliduid");
exit();
}
if(invalidEmail($email) !== false) {
header("location: ../signup.php?error=invalidemail");
exit();
}
if(pwdMatch($pwd, $pwdRepeat) !== false) {
header("location: ../signup.php?error=passwordsdontmatch");
exit();
}
if(Uidexists($conn, $username, $email) !== false) {
header("location: ../signup.php?error=usernametaken");
exit();
}
createUser($conn, $name, $email, $username, $pwd);
}
else {
header("location: ../signup.php");
exit();
}
这是我的functions.inc.php文件的开始,其中出现了未定义的$result。
function emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) {
$result;
if(empty($name) || empty($email) || empty($username) ||
empty($pwd) || empty($pwdrepeat)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
您可以删除函数中的第一个$result并再次检查吗。
function emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) {
if(empty($name) || empty($email) || empty($username) ||
empty($pwd) || empty($pwdrepeat)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
你也可以简写
function emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) {
return (empty($name) || empty($email) || empty($username) ||
empty($pwd) || empty($pwdrepeat)) ?? false;
}