Now that you can insert new employee records and view them, we can now turn our attention on how to update (edit) an existing employee record. A recordset will be created and used to pre-populate a form to "see" existing fields and "edit" ones that needs updating so that they all can be reinserted back to the database. The EDIT EMPLOYEE page is a little more involved than the other pages because you have several processes to handle.
It is helpful to think of an EDIT EMPLOYEE page as a "two-way" street. One way is when a recordset is created to "read" a SINGLE RECORD FROM a database via a SQL SELECT query. The other way is to "write" a SINGLE RECORD TO a database via a SQL UPDATE query. A recordset is created and its dynamic text fields will be created for the text fields in the form so that the existing fields can be pre-populated with a record from the database based on an ID value passed from the EMPLOYEE MASTER PAGE (master_page.php).
An EDIT link on the EMPLOYEE MASTER PAGE (master_page.php) that was created earlier will be used to pass data to this page to build a new recordset and populate the form.
The code that will be created will perform the following tasks:
Static form and form elements will be used from the Add Employee page that was created earlier.
<?php require("db.php");
if(!empty($_POST["add_record"])) {
require_once("db.php");
$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'],
':City'=>$_POST['City'],
':State'=>$_POST['State'],
':StartDate'=>$_POST['StartDate'],
':Note'=>$_POST['Note'] ) );
if (!empty($result) ){
header('location:master_page.php');
}
}
?>
<!doctype html>
<?php
require("db.php");
/* STEP 1: Retrieve a single record to pre-populate form elements */
$pdo_statement = $pdo_conn->prepare("SELECT * FROM employees where ID = 3");
$pdo_statement->execute();
$result = $pdo_statement->fetchAll();
?>
<!doctype html>
<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"
value="<?php echo $result[0]['FirstName']; ?>" ></p>
<p><label for = "LastName">Last Name:</label>
<input type = "text" name = "LastName" id = "LastName" data-mini = "true"
value="<?php echo $result[0]['LastName']; ?>" ></p>
<p><label for = "City">City:</label>
<input type = "text" name = "City" id="City" data-mini = "true"
value="<?php echo $result[0]['City']; ?>" ></p>
<p><label for = "State">State:</label>
<input type = "text" name = "State" id = "State" data-mini = "true"
value="<?php echo $result[0]['State']; ?>" ></p>
<p><label for="StartDate">Start Date:</label>
<input type = "date" name = "StartDate" id = "StartDate" data-mini = "true"
value="<?php echo $result[0]['StartDate']; ?>" ></p>
<p><label for = "Note">Note:</label>
<textarea cols = "40" rows = "8" name ="Note" id = "Note" data-mini = "true" class = "define-textarea">
<?php echo $result[0]['Note']; ?> </textarea></p>
<p><input type = "submit" name = "add_record" value = "SAVE" data-inline = "true"
data-mini = "true" data-icon = "plus" data-iconpos = "left"/></p>
</div>
</form>
CODE EXPLANATION:
- The value attributes are used to pre-populate the form elements.
- It is also important to note that the Note textarea does NOT have a value attribute like the <input> form elements.
The dynamic PHP script is written BETWEEN the opening and closing <textarea> tags and will be rendered as text in
the textarea field. - The $result array index value is zero (e.g., $result[0]['Image'];) for all of the form fields because only ONE
record is returned from the database so the index value will always be zero.
CAUTION: Be careful to not place DOUBLE QUOTES around the URL variable. Also note the period (.) that is used to concatenated the
string with the dynamic variable string.
$pdo_statement = $pdo_conn->prepare("SELECT * FROM employees where ID=" . $_GET[recordID]);
edit_page.php?recordID=28
The first SQL SELECT query was used to per-populate form elements. Now, we need to create a SQL UPDATE query to update any changes that need to be made to a row in the database table.
<?php require_once("db.php"); /* STEP 1: Retrieve a single record to pre-populate form elements */ $pdo_statement = $pdo_conn->prepare("SELECT * FROM employees WHERE ID=" . $_GET[recordID]); $pdo_statement->execute();
$result = $pdo_statement->fetchAll(); /* STEP 2: Insert updated form elements to database */
if(!empty($_POST["add_record"])) {
$pdo_statement=$pdo_conn->prepare("UPDATE employees SET FirstName='" . $_POST[ 'FirstName' ] . "',
LastName='" . $_POST[ 'LastName' ]. "', Title='" . $_POST[ 'Title' ]. "',
City='" . $_POST[ 'City' ]. "',
State='" . $_POST[ 'State' ]. "',
StartDate='" . $_POST[ 'StartDate' ]. "',
Note='" . $_POST[ 'Note' ]. "'
WHERE ID=" . $_GET[recordID]);
$result = $pdo_statement->execute();
if($result) {
header('location:master_page.php');
}
} ?>
<!doctype 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.