PHP必填字段错误
我正试着做一个必填字段。。它会发布所需的错误,但也会发布set_消息。set_消息应仅在成功时显示 但它同时发布了这两个消息。所发生的情况是,如果函数为空,则必填字段不会阻止函数更新PHP必填字段错误,php,mysqli,Php,Mysqli,我正试着做一个必填字段。。它会发布所需的错误,但也会发布set_消息。set_消息应仅在成功时显示 但它同时发布了这两个消息。所发生的情况是,如果函数为空,则必填字段不会阻止函数更新 <?php function recover_password(){ $errors = []; if ($_SERVER['REQUEST_METHOD'] == "POST") { if (isset($_SESSION['token']) &&
<?php
function recover_password(){
$errors = [];
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_SESSION['token']) && $_POST['token'] === $_SESSION['token']) {
$email = clean($_POST['email']);
if (empty($email)) {
$errors[] = "Email Address Required";
}
if (! empty($errors)) {
echo validation_errors($errors[0]);}
if (email_exists($email)) {
$validation_code = generaterandom(12);
setcookie('temp_access_code', $validation_code, time() + 86400);
$sql = "UPDATE users SET validation_code = '" . escape($validation_code) . "' WHERE user_email = '" . escape($email) . "'";
$result = query($sql);
set_message("Instructions to reset your password have been emailed to you");
} else {
echo validation_errors("This email does not exist");
}
} else {
redirect("index.php");
}
}
}
?>
这应该没问题。
你的如果。。。否则就没问题了。
但是语句$errors=[];不是
这是完美的工作,我需要添加另一个else语句我已经更新了代码
function recover_password()
{
$errors = [];
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_SESSION['token']) && $_POST['token'] === $_SESSION['token']) {
$email = clean($_POST['email']);
if (empty($email)) {
$errors[] = "Email Address Required";
}
if (! empty($errors)) {
echo validation_errors($errors[0]);
} else {
if (email_exists($email)) {
$validation_code = generaterandom(12);
setcookie('temp_access_code', $validation_code, time() + 86400);
$sql = "UPDATE users SET validation_code = '" . escape($validation_code) . "' WHERE user_email = '" . escape($email) . "'";
$result = query($sql);
set_message("Instructions to reset your password have been emailed to you");
} else {
echo validation_errors("This email does not exist");
}
}
} else {
redirect("index.php");
}
}
}
解析器遇到的else在哪里?您的第一个代码片段非常不完整。您正在使用HTML5吗?如果是这样,只需向输入中添加一个必需的属性。@JayBlanchard您仍然需要执行服务器端检查,因为客户端检查很容易被绕过。在健壮验证的基础上,将其用作一个很好的增强+
function recover_password()
{
$errors = [];
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_SESSION['token']) && $_POST['token'] === $_SESSION['token']) {
$email = clean($_POST['email']);
if (empty($email)) {
$errors[] = "Email Address Required";
}
if (! empty($errors)) {
echo validation_errors($errors[0]);
} else {
if (email_exists($email)) {
$validation_code = generaterandom(12);
setcookie('temp_access_code', $validation_code, time() + 86400);
$sql = "UPDATE users SET validation_code = '" . escape($validation_code) . "' WHERE user_email = '" . escape($email) . "'";
$result = query($sql);
set_message("Instructions to reset your password have been emailed to you");
} else {
echo validation_errors("This email does not exist");
}
}
} else {
redirect("index.php");
}
}
}