Php 表单验证时不会显示所有错误
我想显示下面所有错误的列表,但它一次只显示一个错误,有人知道我的代码为什么不显示错误列表吗?下面是显示存储错误的数组的代码,用于说明没有错误时会发生什么的代码,以及说明有错误时会发生什么的代码Php 表单验证时不会显示所有错误,php,Php,我想显示下面所有错误的列表,但它一次只显示一个错误,有人知道我的代码为什么不显示错误列表吗?下面是显示存储错误的数组的代码,用于说明没有错误时会发生什么的代码,以及说明有错误时会发生什么的代码 <?php $getcourseid = (isset($_POST['courseid'])) ? $_POST['courseid'] : ''; $getcoursename = (isset($_POST['coursename'])) ? $_POST['coursen
<?php
$getcourseid = (isset($_POST['courseid'])) ? $_POST['courseid'] : '';
$getcoursename = (isset($_POST['coursename'])) ? $_POST['coursename'] : '';
$getduration = (isset($_POST['duration'])) ? $_POST['duration'] : '';
$errors = array();
if (!$getcourseid){
$errors[] = "You must enter in Course's ID";
}else if (!$getcoursename){
$errors[] = "You must enter in Course's Name";
}else if (!$getduration){
$errors[] = "You must select Course's Duration";
}
if(!$errors) {
$insertsql = "
INSERT INTO Course
(CourseNo, CourseName, Duration)
VALUES
(?, ?, ?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("sss", $getcourseid, $getcoursename, $getduration);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
// don't use $mysqli->prepare here
$query = "SELECT CourseNo FROM Course WHERE CourseNo = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$getcourseid);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbCourseId);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();
}
if(empty($errors)) {
if ($numrows == 1){
$errormsg = "<span style='color: green'>Course " . $getcourseid . " - " . $getcoursename . " has been Created</span>";
$getcourseid = "";
$getcoursename = "";
$getduration = "";
}else{
$errormsg = "An error has occured, Course has not been Created";
}
} else {
if (count($errors) > 0)
{
foreach ($errors AS $Errors)
{
$errormsg = "{$Errors} <br>";
}
}
}
?>
**ADDITIONAL QUESTION:**
$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
<table>
<tr>
<td></td>
<td id='errormsg'>$errormsg</td>
</tr>
<tr>
<td>Course ID:</td>
<td><input type='text' name='courseid' value='$getcourseid' /></td>
</tr>
<tr>
<td>Course Name:</td>
<td><input type='text' id='nameofcourse' name='coursename' value='$getcoursename' /></td>
</tr>
<tr>
<td>Duration (Years):</td>
<td>{$durationHTML}</td>
</tr>
<tr>
<td></td>
<td><input type='submit' value='Create Course' name='createbtn' /></td>
</tr>
</table>
</form>";
echo $form;
试试看
试一试
我还没有完成与持续时间相关的代码,您需要弄清楚,我在这里为您提供了输入courseid和coursename的逻辑
<?php
$errors = array();
$getcourseid = "";
$getcoursename = "";
$errormsg = "";
if($_POST)
{
$getcourseid = $_POST['courseid'];
$getcoursename = $_POST['coursename'];
if (!$getcourseid){
$errors['course_id'] = "You must enter in Course's ID";
}if (!$getcoursename){
$errors['course_name'] = "You must enter in Course's Name";
}
}
if(!$errors && $_POST) {
$insertsql = "
INSERT INTO Course
(CourseNo, CourseName, Duration)
VALUES
(?, ?, ?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("sss", $getcourseid, $getcoursename, $getduration);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
// don't use $mysqli->prepare here
$query = "SELECT CourseNo FROM Course WHERE CourseNo = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$getcourseid);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbCourseId);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();
}
$getcourseid = (!empty($getcourseid))?$getcourseid:"";
$getcoursename = (!empty($getcoursename))?$getcoursename:"";
$durationHTML = (!empty($durationHTML))?$durationHTML:"";
$error_c_id = (!empty($errors['course_id']))?$errors['course_id']:"";
$error_c_name = (!empty($errors['course_name']))?$errors['course_name']:"";
$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
<table>
<tr>
<td>Course ID:</td>
<td><input type='text' name='courseid' value='".$getcourseid."' />".$error_c_id."</td>
</tr>
<tr>
<td>Course Name:</td>
<td><input type='text' id='nameofcourse' name='coursename' value='".$getcoursename."' />".$error_c_name."</td>
</tr>
<tr>
<td>Duration (Years):</td>
<td>{$durationHTML}</td>
</tr>
<tr>
<td></td>
<td><input type='submit' value='Create Course' name='createbtn' /></td>
</tr>
</table>
</form>";
echo $form;
?>
我还没有完成与持续时间相关的代码,您需要弄清楚我在这里为您提供了输入courseid和coursename的逻辑
<?php
$errors = array();
$getcourseid = "";
$getcoursename = "";
$errormsg = "";
if($_POST)
{
$getcourseid = $_POST['courseid'];
$getcoursename = $_POST['coursename'];
if (!$getcourseid){
$errors['course_id'] = "You must enter in Course's ID";
}if (!$getcoursename){
$errors['course_name'] = "You must enter in Course's Name";
}
}
if(!$errors && $_POST) {
$insertsql = "
INSERT INTO Course
(CourseNo, CourseName, Duration)
VALUES
(?, ?, ?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("sss", $getcourseid, $getcoursename, $getduration);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
// don't use $mysqli->prepare here
$query = "SELECT CourseNo FROM Course WHERE CourseNo = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$getcourseid);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbCourseId);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();
}
$getcourseid = (!empty($getcourseid))?$getcourseid:"";
$getcoursename = (!empty($getcoursename))?$getcoursename:"";
$durationHTML = (!empty($durationHTML))?$durationHTML:"";
$error_c_id = (!empty($errors['course_id']))?$errors['course_id']:"";
$error_c_name = (!empty($errors['course_name']))?$errors['course_name']:"";
$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
<table>
<tr>
<td>Course ID:</td>
<td><input type='text' name='courseid' value='".$getcourseid."' />".$error_c_id."</td>
</tr>
<tr>
<td>Course Name:</td>
<td><input type='text' id='nameofcourse' name='coursename' value='".$getcoursename."' />".$error_c_name."</td>
</tr>
<tr>
<td>Duration (Years):</td>
<td>{$durationHTML}</td>
</tr>
<tr>
<td></td>
<td><input type='submit' value='Create Course' name='createbtn' /></td>
</tr>
</table>
</form>";
echo $form;
?>
我的坏消息,我必须更改$errormsg=“{$Errors}
”代码>到回显“{$Errors}
”代码>如果我在当前问题下面再问一个问题,而不是创建一个新问题,因为它仍然处理验证问题,你还好吗?@user1881090是的。。。但不是作为评论,因为如果有太多的评论,它会自动鞭挞我的坏作品,我不得不改变$errormsg=“{$Errors}
”代码>到回显“{$Errors}
”代码>如果我在当前问题下面再问一个问题,而不是创建一个新问题,因为它仍然处理验证问题,你还好吗?@user1881090是的。。。但不是作为评论,因为如果有太多的评论,它会自动鞭挞我的坏作品,我不得不改变$errormsg=“{$Errors}
”代码>到回显“{$Errors}
”代码>我在问题中加入了额外的问题我的问题很糟糕,我必须更改$errormsg=“{$Errors}
”代码>到回显“{$Errors}
”代码>我在问题中包含了额外的问题嗨,我在问题的php代码顶部包含了if isset(…)代码,这是否意味着我不需要你if($\u POST)方法?我还需要$getcourseid=(!empty($getcourseid))?$getcourseid:代码>和类似行?请检查我的最新代码。是的,你需要这样做,因为你的错误显示好像关闭了,我可以看到我这边的注意错误。这就是我添加这些行的原因。是的,我没有看到您的更改,您这样做是正确的。您好,如果我只使用php页面顶部的if isset()代码就可以了,这不会导致通知出现问题。非常感谢你。显然,我的答案是错误的。顺便说一句,它会在表单顶部以及每个表单功能旁边显示错误消息。您好,我在问题中的php代码顶部包含了if-isset(…)代码,这是否意味着我不需要您的if($\u POST)方法?我还需要$getcourseid=(!empty($getcourseid))?$getcourseid:代码>和类似行?请检查我的最新代码。是的,你需要这样做,因为你的错误显示好像关闭了,我可以看到我这边的注意错误。这就是我添加这些行的原因。是的,我没有看到您的更改,您这样做是正确的。您好,如果我只使用php页面顶部的if isset()代码就可以了,这不会导致通知出现问题。非常感谢你。显然,我的答案是错误的。顺便说一句,它在表单顶部以及每个表单功能旁边显示错误消息。
<?php
$errors = array();
$getcourseid = "";
$getcoursename = "";
$errormsg = "";
if($_POST)
{
$getcourseid = $_POST['courseid'];
$getcoursename = $_POST['coursename'];
if (!$getcourseid){
$errors['course_id'] = "You must enter in Course's ID";
}if (!$getcoursename){
$errors['course_name'] = "You must enter in Course's Name";
}
}
if(!$errors && $_POST) {
$insertsql = "
INSERT INTO Course
(CourseNo, CourseName, Duration)
VALUES
(?, ?, ?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("sss", $getcourseid, $getcoursename, $getduration);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
// don't use $mysqli->prepare here
$query = "SELECT CourseNo FROM Course WHERE CourseNo = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$getcourseid);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbCourseId);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();
}
$getcourseid = (!empty($getcourseid))?$getcourseid:"";
$getcoursename = (!empty($getcoursename))?$getcoursename:"";
$durationHTML = (!empty($durationHTML))?$durationHTML:"";
$error_c_id = (!empty($errors['course_id']))?$errors['course_id']:"";
$error_c_name = (!empty($errors['course_name']))?$errors['course_name']:"";
$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
<table>
<tr>
<td>Course ID:</td>
<td><input type='text' name='courseid' value='".$getcourseid."' />".$error_c_id."</td>
</tr>
<tr>
<td>Course Name:</td>
<td><input type='text' id='nameofcourse' name='coursename' value='".$getcoursename."' />".$error_c_name."</td>
</tr>
<tr>
<td>Duration (Years):</td>
<td>{$durationHTML}</td>
</tr>
<tr>
<td></td>
<td><input type='submit' value='Create Course' name='createbtn' /></td>
</tr>
</table>
</form>";
echo $form;
?>