我只需要完成此验证的帮助,我有 php 检查用户提交表单时是否有任何错误的地方:
$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();
}
$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;
我的问题是,如果没有错误并且数据库内容已成功完成,我想显示一条成功消息,或者如果表单已完成且没有错误但数据库内容无法完成,则显示一条错误消息。我还想向$errormsg
变量显示所有消息?我能问一下怎么做吗?
以下是我想要实现的目标,但不确定它是否适用于上面的代码:
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";
}