添加标题代码时忽略验证条件

时间:2014-08-12 09:05:24

标签: php mysqli

我有这个注册新客户的表单,我已经对表单的每个字段做了验证条件......除非我尝试将用户重定向到另一个页面,否则一切都好 它会忽略验证条件并传递表格

参见代码

$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 (isset($_POST['subEducation'])) {
    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 = "High School Required";
    } else {
        $lang1degree = check_input($_POST['lang1degree']);
    }

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

    if (empty($_POST['lang2degree'])) {
        $lang2degreeErr = "High School 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);

    $putData->execute();
}

现在我的要求是: 如何在执行这样的查询后重定向用户?

if($putData->execute()){
header("Location:?pid=5&m=p");
}else{
header("Location:?pid=5&m=f");
}

更新 我将代码的最后一部分更新为

if ($putData->execute()) {
        $valid = true;
    } else {
        $valid = false;
    }
    if ($valid == true) {
        header("Location=?pid=4&pp=2&pps=education&m=g");
    }elseif($valid==false){
        header("Location=?pid=4&pp=2&pps=education&m=f");
    }

现在当我尝试上面的代码时会发生什么,它会保留在同一页面而不会重定向用户。

更新

标题格式header("Location=?pid=4&pp=2&pps=education&m=g");应为header("Location:?pid=4&pp=2&pps=education&m=g"); 谢谢大家。

1 个答案:

答案 0 :(得分:1)

我认为您应该检查用户是否填写了所有必需的输入字段,否则它将执行标题。

我发现了这个test if the user has filled all the inputs in a form

希望它有所帮助。