简单表单验证中的多个IF语句


Multiple IF statements in simple form validation

我是一个新手,并试图在阅读后实现一个简单的验证脚本,但我看不出我如何能够有多个if,只会做sql插入,如果满足所有必需的字段。而不是有多个else语句,是什么语法方法让所有的表单验证if在一起,如果其中一个失败,然后显示正确的错误,sql不执行?

if(isset($_POST ['submit'])){
$user_ID = get_current_user_id();
$catErr = $ratingErr = $titleErr = $textErr = "";
if (empty($_POST["category"])) {
$catErr = "Category is required";
} else {
//DO THE INSERT BELOW!
}
if (empty($_POST["rating"])) {
$ratingErr = "Rating is required";
} else {
//DO THE INSERT BELOW!
}
if (empty($_POST["post_name"])) {
$postErr = "Title is required";
} else {
//DO THE INSERT BELOW!
}
if (empty($_POST["text"])) {
$textErr = "Text is required";
} else {
//DO THE INSERT BELOW!
}

//PDO query begins here...
$sql = "INSERT INTO forum(ID,
category,
rating,
post_name,
text

为所有错误消息使用一个变量,并在分支中连接到它,因此,如果最终该变量仍然是空字符串,则不会进行插入。(并且您不需要任何空的else块,它只包含一个注释。)

$err = "";
if (empty($_POST["category"])) {
$err .= "<br/>Category is required";
} 
if (empty($_POST["rating"])) {
$err .= "<br/>Rating is required";
} 
if (empty($_POST["post_name"])) {
$err .= "<br/>Title is required";
} 
if (empty($_POST["text"])) {
$err .= "<br/>Text is required";
}
//PDO query begins here...
if($err=='')
{
$sql = "INSERT INTO forum(ID,
category,
rating,
...";
...
}

你的问题有很多解决办法。这里有三个方法可以解决你的问题。

你可以像这样组合所有的if语句:

if (empty($_POST['rating']) || empty($_POST'rating']) || ... ) { ... }

用双管分隔。

你也可以检查整个数组:

if (empty($_POST)) $error = "There was an error!";

你可以设置一个通用的错误变量,然后输出它。

第三种解决方案可以保留当前的语法,但减少行数。不使用括号可以节省行数。你可以创建一个数组,并把你的错误推入数组。

注意:您可以使用empty()isset()

// create an array to push errors to
$errors_array = array();
// if a particular field is empty then push the relevant error to the array
if(!isset($_POST['category'])) array_push($errors_array, "Category is required");
if(!isset($_POST['rating'])) array_push($errors_array, "Rating is required");
...

一旦你有了一个充满错误的数组,你可以像这样检查它们:

// if the array is not empty (then there are errors! don't insert!)
if (count($errors_array) > 0) {
    // loop through and echo out the errors to the page
    for ($i = 0; $i < count($errors_array); $i++) {
        echo $errors_array[i];
    }
} else {
    // success! run your query!
}

您应该在将页面处理为post之前使用javascript来验证页面。这个脚本将在客户端运行,当他们点击提交并在他们离开页面之前捕获错误。

这里有一个关于如何做这样的事情的教程:

每个字段都可以有自己的验证参数和方法,这也会使页面的代码看起来更好。

在showdev让我这么想之后,我就采用了这种方法。它可能不是很优雅,但是可以达到目的,尽管如果有错误,所有用户都会被带到一个空白页面,并且它简单地说:缺少类别(或其他)。想知道我是否可以从那里的表单回一个链接或一些东西回到页面,这样用户就有一个选项,如"回去并重新提交"。否则,我将不得不处理和显示表单旁边的错误,这将需要一个不同的方法完全…

if(isset($_POST ['submit'])){
$errors = false;
if(empty($_POST['category'])) {
echo 'Missing category.<br>';
$errors = true;
}
if(empty($_POST['rating'])) {
echo 'Missing rating.<br>';
$errors = true;
}
if(empty($_POST['post_name'])) {
echo 'Missing title.<br>';
$errors = true;
}
if(empty($_POST['text'])) {
echo 'Missing text.<br>';
$errors = true;
}
if($errors) {
exit;
}

//然后在这里添加代码。但是如果用户犯了错误,页面上除了错误信息什么也看不见(这就是它现在的工作方式)

通常,如果您发现自己重复编写非常相似的语句,使用某种循环可能是更好的方法。我认为你所说的"处理和显示表单旁边的错误"真的是你需要做的,如果你想要的过程是用户友好的。如果你把你的验证脚本在文件的顶部有你的表单在它,那么你可以只是有表单提交给自己(action="")。如果提交成功,您可以将用户重定向到其他地方,如果没有,他们将再次看到表单,并在有用的地方显示错误消息。

if (isset($_POST['submit'])) {
    // define your required fields and create an array to hold errors
    $required = array('category', 'rating', 'post_name', 'text');
    $errors = array();
    // loop over the required fields array and verify their non-emptiness
    foreach ($required as $field) {
        // Use empty rather than isset here. isset only checks that the 
        // variable exists and is not null, so blank entries can pass.
        if (empty($_POST[$field])) {
            $errors[$field] = "$field is required";
        }
    }
    if (empty($errors)) {
        // insert the record; redirect to a success page (or wherever)
    }
}
// Display the form, showing errors from the $errors array next to the
// corresponding inputs