Before you can update, search, or delete records from a database, you need to add records to it or have a database that already contains records. The ADD EMPLOYEE page will be designed to add a new employee record. This page will be redirected automatically to the EMPLOYEE MASTER PAGE (e.g., master_page.php) once the ADD EMPLOYEE button is clicked.
Although, you can use phpMyAdmin (or any other graphical interface app) to insert records into a database table, it is best practice to create a CUSTOM form to insert records which gives you more control over who can access the database and where they can access the database to view, insert, update, or delete records. Also, anybody with the right privilege can access the database from the web.
Since no recordset is needed for the ADD EMPLOYEE page, it is one of the EASIEST dynamic page to create. However, there is an option in this tutorial on how to create a dynamic combo box from a database table. However, this does requires a recordset.
The code that will be created will perform the following tasks:
Create form and form elements (also known as form variables or form controls) that are needed for the ADD EMPLOYEE page.
<h2 class="ui-body ui-body-a ui-corner-all">ADD EMPLOYEE</h2> <form action = "" method="POST">
<div class = "ui-body ui-body-a ui-corner-all">
<p><label for = "FirstName">First Name:</label>
<input type="text" name = "FirstName" id="FirstName" data-mini = "true" ></p>
<p><label for = "LastName">Last Name:</label>
<input type = "text" name = "LastName" id = "LastName" data-mini = "true" ></p> <p><label for = "Title">Title:</label>
<input type ="text" name = "Title" id = "Title" data-mini = "true" ></p> <p><label for = "City">City:</label>
<input type = "text" name = "City" id="City" data-mini = "true" ></p>
<p><label for = "State">State:</label>
<input type = "text" name = "State" id = "State" data-mini = "true" ></p>
<p><label for="StartDate">Start Date:</label>
<input type = "date" name = "StartDate" id = "StartDate" data-mini = "true" ></p>
<p><label for = "Note">Note:</label>
<textarea cols = "40" rows = "8" name ="Note" id = "Note" data-mini = "true" class = "define-textarea"></textarea></p>
<p><input type = "submit" name = "add_record" value = "ADD EMPLOYEE" data-inline = "true"
data-mini = "true" data-icon = "plus" data-iconpos = "left"/></p>
</div>
</form>
Now that we have the form and form elements created, it is time to add the code that will actually ADD the data from the form to a single row in the database. The following code performs the actions listed below:
<?php require("db.php");
if(!empty($_POST["add_record"])) {
$sql = "INSERT INTO employees ( FirstName, LastName, Title, City, State, StartDate, Note)
VALUES ( :FirstName, :LastName, :Title, :City, :State, :StartDate, :Note)";
$pdo_statement = $pdo_conn->prepare($sql);
$result = $pdo_statement->execute( array (
':FirstName'=>$_POST['FirstName'],
':LastName'=>$_POST['LastName'], ':Title'=>$_POST['Title'],
':City'=>$_POST['City'],
':State'=>$_POST['State'],
':StartDate'=>$_POST['StartDate'],
':Note'=>$_POST['Note'] ) );
}
?> <!doctype html>
<html>
Once we insert a record into the database, we want to redirect the user back to the EMPLOYEE MASTER PAGE (master_page.php).
<?php require("db.php");
if(!empty($_POST["add_record"])) {
$sql = "INSERT INTO employees ( FirstName, LastName, Title, City, State, StartDate, Note)
VALUES ( :FirstName, :LastName, :Title, :City, :State, :StartDate, :Note)";
$pdo_statement = $pdo_conn->prepare($sql);
$result = $pdo_statement->execute( array (
':FirstName'=>$_POST['FirstName'],
':LastName'=>$_POST['LastName'], ':Title'=>$_POST['Title'],
':City'=>$_POST['City'],
':State'=>$_POST['State'],
':StartDate'=>$_POST['StartDate'],
':Note'=>$_POST['Note'] ) );
if (!empty($result) ){
header('location:master_page.php');
}
}
?> <!doctype html>
<html>
You can come back later after you have finished the "MUST HAVE" features of this app with these "NICE TO HAVE" features for this page. See Enhancement tab for details.
When you create a form, you should always validate particular form elements to determine if required form elements have been filled out or that the correct format is given (e.g., email address). There are a host of techniques on how to validate form elements. While minimum validation was done with the required keyword, additional validation can be done with some of the form elements.
It is best practice to DYNIMICALLY populate a menu if the SAME menu will be used on MULTIPLE pages. This way, if there are any changes (additions or deletions) to the menu, it can be done from one table in the database and all of the pages will reflect the changes.
It is best practice create an upload image function when images are needed with the app.