Add Navigation Link for Editing a Student to Student Details Page
Before we create our editstudent.php script, let's first add a navigation link for editing a student to our Student Details page studentdetails.php script. Open up the studentdetails.php script and add the following horizontal line and navigation link just below the closing table tag </table> line:
studentdetails.php - Student Detials: Add Navigation Link to editstudent.php
1 2 3 4 5 6 7 8 910
...
</table><hr/><p>If you would like to change any of the details of this student,feel free to
<ahref='editstudent.php?id_to_edit=<?=$row['id']?>'> edit it</a></p><?php else: ?><h3>No Student Details :-(</h3>
...
Creating a Edit a Student Page with the Usual Bootstrap Template with a Card
Let's create a editstudent.php script using a Bootstrap template and add a Card for our content with a header for the title of the page:
editstudent.php - Edit Student: using Bootstrap Template with Card
1 2 3 4 5 6 7 8 910111213141516171819202122232425
<html><head><linkrel="stylesheet"href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"crossorigin="anonymous"><title>Edit Student</title></head><body><divclass="card"><divclass="card-body"><h1>Edit a Student</h1></div></div><scriptsrc="https://code.jquery.com/jquery-3.3.1.slim.min.js"integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"crossorigin="anonymous"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut"crossorigin="anonymous"></script><scriptsrc="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k"crossorigin="anonymous"></script></body></html>
Connect to the Student Database
We'll need to access the Student database for the student we want to edit:
editstudent.php - Edit Student: Connect to the Database
1 2 3 4 5 6 7 8 910
<body><div class="card"> <div class="card-body"> <h1>Edit a Student</h1><?phprequire_once('dbconnection.php');$dbc=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME)ortrigger_error('Error connecting to MySQL server for DB_NAME.',E_USER_ERROR);
Multiple Ways to Navigate to Edit a Student Page
There are two expected ways and various unanticipated ways to navigate to the editstudent.php script.
The first expected way is for the user to select the edit it link on the Student Details page. Doing so sends a single query parameter contained in the id_to_edit element of the $_GET super global variable.
The second expected way is when the user selects Update Student. When the user successfully enters the required data in the form fields and submits the form, we will want to update the data for this student in the studentListing table of the Student database. You can find the submitted form fields in the $_POST super global variable.
All other mechanisms are unanticipated. So this essentially gives us three choices we are looking for. Therefore, the best way to deal with these three choices is to create an if/elseif/else block of code structure:
editstudent.php - Edit Student: Conditions for Navigating to this Page
1 2 3 4 5 6 7 8 9101112131415161718
$dbc=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME)ortrigger_error('Error connecting to MySQL server for DB_NAME.',E_USER_ERROR);if(isset($_GET['id_to_edit'])){...}elseif(isset($_POST['edit_student_submission'],$_POST['student_first_name'],$_POST['student_last_name'],$_POST['student_email'])){...}else// Unintended page link{...}?>
Direct Navigation from Student Details
The first intended way we will navigate to the editstudent.php script is when the user selects the edit it link on the studentdetails.php page. The link causes an HTTP GET to be sent to the editstudent.php script with the query parameter in the $_GET superglobal variable. We deal with this in the first if block:
12
if(isset($_GET['id_to_edit'])){
They should be presented with a page like this:
In the form above, notice that the fields are pre-populated with the student details. We do this by querying the studentListing table for the student details before displaying the form.
Inside the if block, query the studentListing table for the student detials we want to edit using the id_to_edit query parameter by adding the following code:
editstudent.php - Edit Student: direct navigation to this Page from Student Details
1 2 3 4 5 6 7 8 910111213141516171819
if(isset($_GET['id_to_edit'])){$id_to_edit=$_GET['id_to_edit'];$query="SELECT * FROM studentListing WHERE id = $id_to_edit";$result=mysqli_query($dbc,$query)ortrigger_error('Error querying database studentListing',E_USER_ERROR);if(mysqli_num_rows($result)==1){$row=mysqli_fetch_assoc($result);$student_first_name=$row['first_name'];$student_last_name=$row['last_name'];$student_email=$row['email'];}}
Display the Edit Form with Sticky Fields
Before working on the other conditions, let's add the form to update the student below all the conditional logic:
editstudent.php - Edit Student: Form for Updateing Student Details
else // Unintended page link - No student to edit, link back to index
{
header("Location: index.php");
}
?>
<formclass="needs-validation"novalidatemethod="POST"action="<?= $_SERVER['PHP_SELF'] ?>"><divclass="form-group row"><labelfor="student_first_name"class="col-sm-3 col-form-label-lg">First Name</label><divclass="col-sm-8"><inputtype="text"class="form-control"id="student_first_name"name="student_first_name"value='<?=$student_first_name?>'placeholder="First Name"required><divclass="invalid-feedback">
Please provide a valid first name.
</div></div></div><divclass="form-group row"><labelfor="student_last_name"class="col-sm-3 col-form-label-lg">Last Name</label><divclass="col-sm-8"><inputtype="text"class="form-control"id="student_last_name"name="student_last_name"value='<?=$student_last_name?>'placeholder="Last Name"required><divclass="invalid-feedback">
Please provide a valid last name.
</div></div></div><divclass="form-group row"><labelfor="student_email"class="col-sm-3 col-form-label-lg">Email</label><divclass="col-sm-8"><inputtype="email"class="form-control"id="student_email"name="student_email"value='<?=$student_email?>'placeholder="Email"required><divclass="invalid-feedback">
Please provide a valid email address.
</div></div></div><buttonclass="btn btn-primary"type="submit"name="edit_student_submission">Update Student</button><inputtype="hidden"name="id_to_update"value="<?= $id_to_edit ?>"></form>
Notice above that the fields have been made "sticky".
Also notice above the hidden<input> element witht the name attribute set to id_to_update so we know which ID to update in the database.
Finally, also notice we are using Bootstrap's client side validation, so add the following JavaScript below the form:
1 2 3 4 5 6 7 8 9101112131415161718192021
</form><script>// JavaScript for disabling form submissions if there are invalid fields(function(){'use strict';window.addEventListener('load',function(){// Fetch all the forms we want to apply custom Bootstrap validation styles tovarforms=document.getElementsByClassName('needs-validation');// Loop over them and prevent submissionvarvalidation=Array.prototype.filter.call(forms,function(form){form.addEventListener('submit',function(event){if(form.checkValidity()===false){event.preventDefault();event.stopPropagation();}form.classList.add('was-validated');},false);});},false);})();</script>
Navigation as a Result of Pressing the Update Button
This is the second intended way we will navigate to the editstudent.php page. Therefore we will be dealing with the elseif block:
We're in this condition as a result of the user selecting the Update Student submit button.
Because the web is stateless, we needed to carry over the student id we received from the previous navigation to the editstudent.php script (which we received as a query parameter from the $_GET[] superglobal). We took that parameter and assigned it to a variable called $id_to_edit in the last HTTP transaction and assigning it to a hidden variable in the form which we can now access in the $_POST[] superglobal:
In this elesif condition, we will update the student details in the studentListing table of the Student database. Add the following code within the elseif block:
editstudent.php - Edit Student: Navigation from Pressing the Update Student Button
1 2 3 4 5 6 7 8 910111213141516171819202122
elseif(isset($_POST['edit_student_submission'],$_POST['student_first_name'],$_POST['student_last_name'],$_POST['student_email'],$_POST['id_to_update'])){$student_first_name=$_POST['student_first_name'];$student_last_name=$_POST['student_last_name'];$student_email=$_POST['student_email'];$id_to_update=$_POST['id_to_update'];$query="UPDATE studentListing SET first_name = '$student_first_name', "." last_name = '$student_last_name', email = '$student_email' "."WHERE id = $id_to_update";mysqli_query($dbc,$query)ortrigger_error('Error querying database studentListing: Failed to update student listing',E_USER_ERROR);$nav_link='studentdetails.php?id='.$id_to_update;header("Location: $nav_link");}
Notice when we are done updating the database, we link back to the Student Details page.
Unanticipated Navigation Methods
As before, since the HTTP request is coming from the client. In theory, unanticipated or malicious parameters can be in the query string (or even field parameters from the HTTP POST). Therefore, all other conditions we could navigate to the editstudent.php page are unintended, and we will be dealing with the else block:
12
else// Unintended page link{
In this case, we just want to link back to the main Student Listing main index.php page:
1234
else// Unintended page link - No student to edit, link back to index{header("Location: index.php");}