I have a piece of code below where it performs a couple of queries and if there is a row found from one of the first two queries, then it will display validation errors, if there are no rows found form the 2 queries then it performs an insert and another SELECT query:
// don't use $mysqli->prepare here
$query = "SELECT StudentUsername FROM Student WHERE StudentUsername = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$getusername);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbStudentUsername);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();
$aliasquery = "SELECT StudentAlias FROM Student WHERE StudentAlias = ?";
// prepare query
$aliasstmt=$mysqli->prepare($aliasquery);
// You only need to call bind_param once
$aliasstmt->bind_param("s",$getalias);
// execute query
$aliasstmt->execute();
// get result and assign variables (prefix with db)
$aliasstmt->bind_result($dbStudentAlias);
//get number of rows
$aliasstmt->store_result();
$aliasnumrows = $aliasstmt->num_rows();
if ($aliasnumrows == 0){
if ($numrows == 0){
$formatdate = date("Y-m-d",strtotime($getdob));
$studentpassword = md5(md5("93w".$studentpassword."ed0"));
$insertsql = "
INSERT INTO Student
(StudentForename, StudentSurname, StudentAlias, StudentUsername, StudentPassword, StudentDOB, Year, CourseId)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("ssssssis", $getfirstname, $getsurname, $getalias, $getusername, $studentpassword, $formatdate, $getyear, $getcourse);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
// don't use $mysqli->prepare here
$query = "SELECT StudentUsername FROM Student WHERE StudentUsername = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$getusername);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbStudentUsername);
//get number of rows
$stmt->store_result();
$numrowsstmt = $stmt->num_rows();
}else{
$errors['username'] = "<span style='color: red'>There is already a Student with that Username</span>";
}
}else{
$errors['alias'] = "<span style='color: red'>There is already a Student with that Alias</span>";
}
}
The problem I am having though is that I want to display both the validation errors at the same time if both validation errors are met. At the moment it is only displaying the validation errors one at a time. How can I get the both validation errors displayed together if both validation errors are met?
Below is the code where it stores the validation errors in the form:
$error_alias= (!empty($errors['alias']))?$errors['alias']:"";
$error_username = (!empty($errors['username']))?$errors['username']:"";
$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
<table>
<tr>
<td>Alias:</td>
<td><input type='text' name='alias' value='$getalias' /><br/>".$error_alias."</td>
</tr>
<tr>
<td>Username:</td>
<td><input type='text' name='studentusername' value='$getusername' /><br/>".$error_username."</td>
</tr>
<tr>
<td></td>
<td><input type='submit' value='Register' name='registerbtn' /></td>
</tr>
</table>
</form>";
echo $form;
You can change the if statement slightly: