0

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

so what I done is

    <?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());
}
?>

And this is how the html form look like

<label for="otherUniversity">Other</label>
    <input name="otherUniversity" type="text" id="otherUniversity" value="<?php echo $otherUniversity ?>">
    <span class="notAllowed"><?php echo $otherUniversityErr ?></span>

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)

If you need to I provide more code on this please let me know.

Update I asked about how to pull out the error from the array and this is what I did to do so.

<?php if(isset($error)){ echo $error["FirstUniversityDegreeErr"];} ?>
3
  • Have you tried separating all of your checks out into a separate function called something like 'is_invalid` that either constructs your error message or returns false if it is not invalid? If the return is false (no errors), load in the db; if not (there's a message, so true), print the message. At the very least, it might make your code a little more maintainable and easier to debug. Just a suggestion. Commented Aug 17, 2014 at 11:25
  • With code you provided if your database returns exception nothing should insert. What is error message? Is it in database level or php level? Commented Aug 17, 2014 at 11:40
  • #masoud keshavarz it's not about a error message there is no error appear it just proceed and insert a new row which is empty into my database ... it should stop and display the error under the field without inserting anything in the db Commented Aug 17, 2014 at 11:47

3 Answers 3

2

You can simply add the error array to validate your form

If you read your code you see the reason behind you are inserting value in the database is your inserting query does not rely on the error, it simply gonna run when you click on submit.

So what you have to do is to add if statement before you run the query which checks whether you got some errors or not, if error is empty then run this query other wise do some thing else

to do this simply add this line in your script at the top

$errors = array();//it will be the $errors array which we will check before we run insert query

After that replace your error messages with some thing like this I am changing your two error messages then you will know the drill

$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';
Sign up to request clarification or add additional context in comments.

6 Comments

Please update your previous answer instead of writing multiple answers.
Don't delete your post it decrease your reputation :) Its better to update your answers when you want to say something new to OP :)
OK it seems that that is the best idea I got for now but how I can display the error under every field
@user3353181 that is a different question and you must ask it in a new question but for now you can display your hidden div tag with javascript, div tages must be placed under fields, read this question stackoverflow.com/questions/14303780/…
#Arif_suhail_123 any idea how to pull out the error message from the array and display it under every field
|
0

I assume that the validation is in a function. It should: return false; After each of the error statements to prevent post. Otherwise, need more info to help you out.

Comments

0

Add this line of codes before your insert command:

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

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

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

   .
   .
   .

When you get it user inserted an invalid value you must halt process with return;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.