php和header中的验证表单

php和header中的验证表单,php,validation,Php,Validation,我试图验证我的客户资料表,检查他是否填写了所有字段,并确保他填写了有效数据,如果所有字段都有效,则将数据发送到数据库 所以我所做的就是 <?php error_reporting(E_ALL); ini_set('display_errors', 1); $FirstUniversityDegree = $university = $otherUniversity = $faculty = $otherFaculty = $major = $otherMajor = $

我试图验证我的客户资料表,检查他是否填写了所有字段,并确保他填写了有效数据,如果所有字段都有效,则将数据发送到数据库

所以我所做的就是

    <?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

$FirstUniversityDegree = $university = $otherUniversity = $faculty = $otherFaculty = $major = $otherMajor
    = $univGrad = $gradProject = $graduationYear = $highSchool = $schoolGrad = $highestUnivDegree = $otherHighEdu
    = $research = $educationYear = $educationGrad = $lang1 = $lang1degree = $lang2 = $lang2degree = $lang3 = $lang3degree
    = $lang4 = "";

$FirstUniversityDegreeErr = $universityErr = $otherUniversityErr = $facultyErr = $otherFacultyErr = $majorErr
    = $otherMajorErr = $univGradErr = $gradProjectErr = $graduationYearErr = $highSchoolErr = $schoolGradErr
    = $highestUnivDegreeErr = $otherHighEduErr = $researchErr = $educationYearErr = $educationGradErr = $lang1Err
    = $lang1degreeErr = $lang2Err = $lang2degreeErr = $lang3Err = $lang3degreeErr = $lang4Err = "";

$id = "";
$uid = "";

if ($_SERVER['REQUEST_METHOD']=="POST") {
    if (empty($_POST['FirstUniversityDegree'])) {
        $FirstUniversityDegreeErr = "First University Degree Required";
    } else {
        $FirstUniversityDegree = check_input($_POST['FirstUniversityDegree']);
    }

    if (empty($_POST['university'])) {
        $universityErr = "University Required";
    } else {
        $university = check_input($_POST['university']);
    }

    $otherUniversity = check_input($_POST['otherUniversity']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $otherUniversity)) {
        $otherUniversityErr = "Only letters, numbers and '_' allowed";
    }

    if (empty($_POST['faculty'])) {
        $facultyErr = "Faculty Required";
    } else {
        $faculty = check_input($_POST['faculty']);
    }

    $otherFaculty = check_input($_POST['otherFaculty']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $otherFaculty)) {
        $otherFacultyErr = "Only letters, numbers and '_' allowed";
    }

    if (empty($_POST['major'])) {
        $majorErr = "Major Required";
    } else {
        $major = check_input($_POST['major']);
    }

    $otherMajor = check_input($_POST['otherMajor']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $otherMajor)) {
        $otherMajorErr = "Only letters, numbers and '_' allowed";
    }

    if (empty($_POST['univGrad'])) {
        $univGradErr = "Grade Required";
    } else {
        $univGrad = check_input($_POST['univGrad']);
    }

    $gradProject = check_input($_POST['gradProject']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $gradProject)) {
        $gradProjectErr = "Only letters, numbers and '_' allowed";
    }

    $graduationYear = check_input($_POST['graduationYear']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $graduationYear)) {
        $graduationYearErr = "Only letters, numbers and '_' allowed";
    }

    if (empty($_POST['highSchool'])) {
        $highSchoolErr = "High School Required";
    } else {
        $highSchool = check_input($_POST['highSchool']);
    }

    $schoolGrad = check_input($_POST['schoolGrad']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $schoolGrad)) {
        $schoolGradErr = "Only letters, numbers and '_' allowed";
    }

    $highestUnivDegree = check_input($_POST['highestUnivDegree']);

    $otherHighEdu = check_input($_POST['otherHighEdu']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $otherHighEdu)) {
        $otherHighEduErr = "Only letters, numbers and '_' allowed";
    }

    $research = check_input($_POST['research']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $research)) {
        $researchErr = "Only letters, numbers and '_' allowed";
    }

    $educationYear = check_input($_POST['educationYear']);
    if (!preg_match("/^[0-9_a-zA-Z ]*$/", $educationYear)) {
        $educationYearErr = "Only letters, numbers and '_' allowed";
    }

    $educationGrad = check_input($_POST['educationGrad']);

    if (empty($_POST['lang1'])) {
        $lang1Err = "Language 1 Required";
    } else {
        $lang1 = check_input($_POST['lang1']);
    }

    if (empty($_POST['lang1degree'])) {
        $lang1degreeErr = "Degree Required";
    } else {
        $lang1degree = check_input($_POST['lang1degree']);
    }

    if (empty($_POST['lang2'])) {
        $lang2Err = "Language 2 Required";
    } else {
        $lang2 = check_input($_POST['lang2']);
    }

    if (empty($_POST['lang2degree'])) {
        $lang2degreeErr = "Degree Required";
    } else {
        $lang2degree = check_input($_POST['lang2degree']);
    }

    $lang3 = check_input($_POST['lang3']);
    $lang3degree = check_input($_POST['lang3degree']);
    $lang4 = check_input($_POST['lang4']);

    $putData = $db->prepare("INSERT INTO hired_education_info(id, uid, first_university_degree, university, faculty,
    other_faculty, major, other_major, university_grad, grad_project, graduation_year, high_school_degree,
    other_high_school_degree, highest_university_degree, other_highest_university_degree, field_of_research,
    gs_graduation_year, gs_grade, lang1, lang1degree, lang2, lang2degree, lang3, lang3degree, other_lang)
    VALUE(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $putData->bind_param('iisssssssssssssssssssssss', $id, $uid, $FirstUniversityDegree, $university, $faculty,
        $otherFaculty, $major, $otherMajor, $univGrad, $gradProject, $graduationYear, $highSchool, $schoolGrad,
        $highestUnivDegree, $otherHighEdu, $research, $educationYear, $educationGrad, $lang1, $lang1degreeErr,
        $lang2, $lang2degree, $lang3, $lang3degree, $lang4);

    if ($putData->execute());
}
?>
问题是,当我尝试提交表单时,会出现错误消息,但同时它会继续并在我的数据库中插入一个新行,这是错误的,它应该在出现此错误时停止

如果你需要我提供更多的代码,请让我知道

更新 我询问如何从数组中提取错误 这就是我所做的

<?php if(isset($error)){ echo $error["FirstUniversityDegreeErr"];} ?>

我假设验证是在函数中进行的。它应当: 返回false;
在每个错误语句之后防止post。否则,需要更多信息来帮助您

在insert命令之前添加这行代码:

   IF ($FirstUniversityDegree == ""){
       echo $FirstUniversityDegreeErr;
       return;
   }

   IF ($university == ""){
       echo $universityErr;
       return;
   }

   IF ($university == ""){
       echo $universityErr;
       return;
   }

   .
   .
   .

当用户插入一个无效值时,必须使用
return停止进程

您只需添加错误数组即可验证表单

如果您阅读了代码,您会发现在数据库中插入值的原因是您的插入查询不依赖于错误,当您单击submit时,它将运行

因此,您需要做的是在运行查询之前添加if语句,该语句检查您是否有错误,如果错误为空,则以其他方式运行此查询并执行其他操作

要做到这一点,只需在脚本顶部添加这一行

$errors = array();//it will be the $errors array which we will check before we run insert query
在那之后,用类似这样的东西替换你的错误消息,我正在更改你的两条错误消息,这样你就知道了

$FirstUniversityDegreeErr = "First University Degree Required";
 $universityErr = "University Required";
//change them like this 

$error['FirstUniversityDegreeErr']="First University Degree Required";
$error['universityErr']="University Required";

//then the line before you run your query checks whether your array is empty or not 

//then run the query other else show what ever message you want to show like this 

if(!errors)
{
$putData = $db->prepare("INSERT INTO hired_education_info(id, uid, first_university_degree, university, faculty,
    other_faculty, major, other_major, university_grad, grad_project, graduation_year, high_school_degree,
    other_high_school_degree, highest_university_degree, other_highest_university_degree, field_of_research,
    gs_graduation_year, gs_grade, lang1, lang1degree, lang2, lang2degree, lang3, lang3degree, other_lang)
    VALUE(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $putData->bind_param('iisssssssssssssssssssssss', $id, $uid, $FirstUniversityDegree, $university, $faculty,
        $otherFaculty, $major, $otherMajor, $univGrad, $gradProject, $graduationYear, $highSchool, $schoolGrad,
        $highestUnivDegree, $otherHighEdu, $research, $educationYear, $educationGrad, $lang1, $lang1degreeErr,
        $lang2, $lang2degree, $lang3, $lang3degree, $lang4);

    if ($putData->execute());

//so what if statement is doing here is checking for the error messages if the error messages are empty then it will run the query other wise means there are error message it wont run the query. and we can display the message in else statement like this 

}else 

echo 'see the error message';

您是否尝试过将所有签出分离到一个单独的函数中,该函数名为“is_invalid”,用于构造错误消息或返回
false
(如果它不是无效的话)?如果返回值为false(无错误),则在db中加载;如果没有(有消息,所以是真的),打印消息。至少,它可能会使代码更易于维护和调试。只是一个建议。如果数据库返回异常,则使用您提供的代码不应插入任何内容。什么是错误消息?是数据库级还是php级?#masoud keshavarz这不是关于错误消息没有错误出现它只是继续并在我的数据库中插入一个空的新行。。。它应该停止并在字段下显示错误,而不在数据库中插入任何内容。请更新您以前的答案,而不是写多个答案。不要删除您的帖子,这会降低您的声誉:)当您想对OP说一些新的内容时,最好更新您的答案:)好的,这似乎是我现在得到的最好的主意,但是如何在每种情况下显示错误field@user3353181这是一个不同的问题,您必须在一个新问题中提问,但现在您可以使用javascript显示隐藏的div标记,div标记必须放在字段下,阅读这个问题#Arif_suhail_123知道如何从数组中提取错误消息并在每个字段下显示它吗
$FirstUniversityDegreeErr = "First University Degree Required";
 $universityErr = "University Required";
//change them like this 

$error['FirstUniversityDegreeErr']="First University Degree Required";
$error['universityErr']="University Required";

//then the line before you run your query checks whether your array is empty or not 

//then run the query other else show what ever message you want to show like this 

if(!errors)
{
$putData = $db->prepare("INSERT INTO hired_education_info(id, uid, first_university_degree, university, faculty,
    other_faculty, major, other_major, university_grad, grad_project, graduation_year, high_school_degree,
    other_high_school_degree, highest_university_degree, other_highest_university_degree, field_of_research,
    gs_graduation_year, gs_grade, lang1, lang1degree, lang2, lang2degree, lang3, lang3degree, other_lang)
    VALUE(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $putData->bind_param('iisssssssssssssssssssssss', $id, $uid, $FirstUniversityDegree, $university, $faculty,
        $otherFaculty, $major, $otherMajor, $univGrad, $gradProject, $graduationYear, $highSchool, $schoolGrad,
        $highestUnivDegree, $otherHighEdu, $research, $educationYear, $educationGrad, $lang1, $lang1degreeErr,
        $lang2, $lang2degree, $lang3, $lang3degree, $lang4);

    if ($putData->execute());

//so what if statement is doing here is checking for the error messages if the error messages are empty then it will run the query other wise means there are error message it wont run the query. and we can display the message in else statement like this 

}else 

echo 'see the error message';