PHP必填字段错误

时间:2016-01-19 17:20:11

标签: php mysqli

我正在尝试创建一个字段..它发布了所需的错误,但它也发布了set_message,set_message只应在成功时显示。

但它同时发布。发生了什么事情是必要的字段不是停止功能更新,如果它的空白

<?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");
        }
    }
}
?>

2 个答案:

答案 0 :(得分:1)

这应该没问题。 你的if ... else结构似乎没问题。 但语句 $ errors = []; 不是

<?php 
function recover_password(){
    $errors = array();
    if ($_SERVER['REQUEST_METHOD'] == "POST") {
        if (isset($_SESSION['token']) && $_POST['token'] === $_SESSION['token']) {
            $email = clean($_POST['email']);
            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");
        }
    }
}
?>

答案 1 :(得分:0)

这非常有效我需要添加另一个声明我已经更新了代码......

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");
        }
    }
}