我正在尝试验证我的客户端配置文件表单并检查他是否填写了所有字段,并确保他填写了有效数据,如果所有字段都有效,则将数据发送到数据库
所以我做的是
<?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());
}
?>
这就是html
形式的样子
<label for="otherUniversity">Other</label>
<input name="otherUniversity" type="text" id="otherUniversity" value="<?php echo $otherUniversity ?>">
<span class="notAllowed"><?php echo $otherUniversityErr ?></span>
问题是当我尝试提交表单时出现错误消息,但同时它继续并向我的数据库中插入一个新行,这是错误的,应该停止此错误
如果您需要我提供更多相关代码,请告知我们。
更新 我问过如何从数组中提取错误 这就是我这样做的。
<?php if(isset($error)){ echo $error["FirstUniversityDegreeErr"];} ?>
答案 0 :(得分:2)
您只需添加错误数组即可验证表单
如果您阅读了代码,您会看到在数据库中插入值后面的原因是您的插入查询不依赖于错误,它只是在您单击提交时运行。
所以你需要做的是在运行查询之前添加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';
答案 1 :(得分:0)
我假设验证在一个函数中。这应该: 返回false; 每个错误语句后都要防止发帖。否则,需要更多信息来帮助您。
答案 2 :(得分:0)
在插入命令之前添加以下代码行:
IF ($FirstUniversityDegree == ""){
echo $FirstUniversityDegreeErr;
return;
}
IF ($university == ""){
echo $universityErr;
return;
}
IF ($university == ""){
echo $universityErr;
return;
}
.
.
.
当你得到它时,用户插入了一个无效值,你必须用return;