Php 表单的后端验证被忽略
我试图验证输入到表单提交中的数据,我正在使用PHP处理表单提交,首先是为了确保字段已填充,然后是为了确保数据正确,但由于某种原因,所有这些似乎都被忽略了,无论我尝试做什么,数据都会提交到DBPhp 表单的后端验证被忽略,php,Php,我试图验证输入到表单提交中的数据,我正在使用PHP处理表单提交,首先是为了确保字段已填充,然后是为了确保数据正确,但由于某种原因,所有这些似乎都被忽略了,无论我尝试做什么,数据都会提交到DB // example of validation (this is just duplicated for 4 fields) if (isset($_POST["fullname"]) && !empty($_POST["fullname"])) { header('Location:
// example of validation (this is just duplicated for 4 fields)
if (isset($_POST["fullname"]) && !empty($_POST["fullname"])) {
header('Location: http://mywebsite.com/error');
} else {
// Trying to get it to redirect at all
header('Location: http://mywebsite.com/error');
}
$email = $_POST['email'];
// using google code email validation
include('EmailAddressValidator.php');
$validator = new EmailAddressValidator;
if ($validator->check_email_address($email)) {
// Email address is technically valid
} else {
header('Location: http://mywebsite.com/error');
}
// If this line is reached, submit the data to the DB but there's no way this line should be reached because the very first if & else statement should send it to the error page.
任何帮助都将不胜感激!在这一点上,我们已经尝试了几个小时来解决这个问题。尝试添加
退出代码>在使用标头重定向后。请参见下面的示例:
header('Location: http://mywebsite.com/error');exit;
以下是您应如何提交表格的示例:
// example of validation (this is just duplicated for 4 fields)
if (isset($_POST["fullname"]) && isset($_POST["email"])) {
// using google code email validation
include('EmailAddressValidator.php');
$validator = new EmailAddressValidator;
$email = $_POST['email'];
if (empty($_POST["fullname"]) && !$validator->check_email_address($email)) {
header('Location: http://mywebsite.com/error');exit;
} else {
// enter into data here
}
}
首先生成第一个if或not和…if(设置($_POST[“fullname”])| |!空($_POST[“fullname”]))
第二,你必须说如果($validator->check_email_address($email))
//插入DB带标头的重定向不会停止进程,它只发送一个标头,addexit()代码>如果您希望流程完成。您不需要设置和!空的
,!空的
就足够了。@JonStirling已经把手指放在上面了。header()语句不会停止执行。您的代码一直运行到header()之后。如果要在header()之后停止执行,必须使其停止。@JonStirling,但是否应该将我重新定向到错误页?以前任何时候我都使用过相同的代码。为什么要将其设置为或?因为我认为您不需要空用户名…并且可以设置带有空格值“”的用户名。我会假设相反的情况,因为条件是按此方式编写的。在您的示例中,只要设置了键,它就会通过。。。如果它没有设置,那么它总是空的。是不是故意错过了退出的呢?@JonStirling补充道:)