title: validation form in php and header
body: <p>I am trying to validate my client profile form and check if he have filled all the fields and ensure that he filled them with a valid data and if all of them were valid send the data to the Database</p>
  
  <p>so what I done is</p>
  
  <pre><code>    &lt;?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-&gt;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-&gt;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-&gt;execute());
  }
  ?&gt;
  </code></pre>
  
  <p>And this is how the <code>html</code> form look like</p>
  
  <pre><code>&lt;label for="otherUniversity"&gt;Other&lt;/label&gt;
      &lt;input name="otherUniversity" type="text" id="otherUniversity" value="&lt;?php echo $otherUniversity ?&gt;"&gt;
      &lt;span class="notAllowed"&gt;&lt;?php echo $otherUniversityErr ?&gt;&lt;/span&gt;
  </code></pre>
  
  <p>Problem is when I try to submit the form the error messages appear but in the same time it proceed and insert a new row into my db which is wrong, it should stop on this error(s) </p>
  
  <p>If you need to I provide more code on this please let me know.</p>
  
  <p><strong>Update</strong>
  I asked about how to pull out the error from the array
  and this is what I did to do so.</p>
  
  <pre><code>&lt;?php if(isset($error)){ echo $error["FirstUniversityDegreeErr"];} ?&gt;
  </code></pre>