Note that there are some explanatory texts on larger screens.

plurals
  1. POdrop down menu goes back to displaying "Please Select" and fail/success message not appearing
    primarykey
    data
    text
    <p>I am having two problems with my code below.</p> <pre><code> &lt;?php $validSubmission = isset($_POST['resetpass']) &amp;&amp; $_POST['students'] &amp;&amp; $_POST['newpass'] &amp;&amp; $_POST['confirmpass']; $sql = "SELECT StudentUsername, StudentForename, StudentSurname FROM Student ORDER BY StudentUsername"; $sqlstmt = $mysqli-&gt;prepare($sql); $sqlstmt-&gt;execute(); $sqlstmt-&gt;bind_result($dbStudentUsername, $dbStudentForename, $dbStudentSurname); $students = array(); // easier if you don't use generic names for data $studentHTML = ""; $studentHTML .= '&lt;select name="students" id="studentsDrop"&gt;' . PHP_EOL; $studentHTML .= '&lt;option value=""&gt;Please Select&lt;/option&gt;' . PHP_EOL; $outputstudent = ""; while ($sqlstmt-&gt;fetch()) { $student = $dbStudentUsername; $firstname = $dbStudentForename; $surname = $dbStudentSurname; if (!$validSubmission &amp;&amp; isset($_POST['students']) &amp;&amp; $student == $_POST['students']) { $studentHTML .= "&lt;option value='" . $student . "' selected='selected'&gt;" . $student . " - " . $firstname . " " . $surname . "&lt;/option&gt;" . PHP_EOL; } else { $studentHTML .= "&lt;option value='" . $student . "'&gt;" . $student . " - " . $firstname . " " . $surname . "&lt;/option&gt;" . PHP_EOL; } } $studentHTML .= '&lt;/select&gt;'; $errormsg = (isset($errormsg)) ? $errormsg : ''; if (isset($_POST['resetpass'])) { //get the form data $studentdrop = (isset($_POST['students'])) ? $_POST['students'] : ''; $newpass = (isset($_POST['newpass'])) ? $_POST['newpass'] : ''; $confirmpass = (isset($_POST['confirmpass'])) ? $_POST['confirmpass'] : ''; //make sure all data was entered if ($studentdrop != "") { if ($newpass) { if (strlen($newpass) &lt;= 5) { $errormsg = "Your Password must be a minimum of 6 characters or more"; } else { if ($confirmpass) { if ($newpass === $confirmpass) { //Make sure password is correct $query = "SELECT StudentUsername FROM Student WHERE StudentUsername = ?"; // prepare query $stmt = $mysqli-&gt;prepare($query); // You only need to call bind_param once $stmt-&gt;bind_param("s", $username); // execute query $stmt-&gt;execute(); // get result and assign variables (prefix with db) $stmt-&gt;bind_result($dbStudentUsername); //get number of rows $stmt-&gt;store_result(); $numrows = $stmt-&gt;num_rows(); if ($numrows == 1) { //encrypt new password $newpassword = md5(md5("93w" . $newpass . "ed0")); //update the db $updatesql = "UPDATE Student SET StudentPassword = ? WHERE StudentUsername = ?"; $update = $mysqli-&gt;prepare($updatesql); $update-&gt;bind_param("ss", $newpassword, $username); $update-&gt;execute(); //make sure the password is changed $query = "SELECT StudentUsername, StudentPassword FROM Student WHERE StudentUsername = ? AND StudentPassword = ?"; // prepare query $stmt = $mysqli-&gt;prepare($query); // You only need to call bind_param once $stmt-&gt;bind_param("ss", $username, $newpassword); // execute query $stmt-&gt;execute(); // get result and assign variables (prefix with db) $stmt-&gt;bind_result($dbStudentUsername, $dbStudentPassword); //get number of rows $stmt-&gt;store_result(); $numrows = $stmt-&gt;num_rows(); if ($numrows == 1) { $errormsg = "&lt;span style='color: green'&gt;Student " . $student . " - " . $firstname . " " . $surname . " has been Registered&lt;/span&gt;"; } else { $errormsg = "An error has occured, the Password was not Reset"; } } } else { $errormsg = "Your New Password did not Match"; } } else { $errormsg = "You must Confirm your New Password"; } } } else { $errormsg = "You must Enter your New Password"; } } else if ($studentdrop == "") { $errormsg = "You must Select a Student"; } } </code></pre> <p>I am trying to create a rest password page where an admin can reset a student's password.</p> <p><strong>PROBLEM 1:</strong> </p> <p>In my code what I am trying to do is that if a php validation message appears (one of the <code>$errormsg</code> appears except for the <code>$errormsg</code> which displays the sucess message), then the <code>students</code> drop down menu should still display the option that was selected after the submission of the form occurs. Now this works for all the validation message where the user has left a text input blank, but the only validation message it doesn't work for is when the user has not typed in matching passwords for the new and confirm passwords. If the <code>$errormsg = "Your New Password did not Match"; </code> occurs then the students drop down menu goes back to the <code>Please Select</code> option. How come it goes back to the <code>Please Select</code> option everytime this validation message appears and how can I keep the selected student still selected if this validation occurs?</p> <p><strong>PROBLEM 2:</strong> </p> <p>If I successfully enter in all the details and submit, it does not perform the insert, yet it does not display the fail message <code>$errormsg = "An error has occured, the Password was not Reset";</code> or the success message <code>$errormsg = "&lt;span style='color: green'&gt;Student " . $student . " - " . $firstname . " ". $surname . " has been Registered&lt;/span&gt;";</code>, why is this occuring? I know the UPDATE statement is correct as I tested this in phpmyadmin.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload