I want to display a list of all the errors below but instead it is only displaying one error at a time, does anyone know why my code is not displaying a list of errors? Below is the code display the array to store the errors, the code for stating what happens if there is are no errors, and the code stating what happens if there are errors.
<?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;
The above form goes below the php code at the top of this question. Now what is happening is that the list of validation errors are all being displayed at the top of the form. But what I really want is each validation error to go below the form feature it is referring to.
E.g If I have not filled in the Course Id text input, then I want the validation error "You must enter in Course's ID" to be displayed below the Course Id text input.
If I have not filled in the Course Name text input, then I want the validation error "You must enter in Course's Name" to be displayed below the Course Name text input.
etc. What needs to be changed in code in order to do this?
try