Now that HR can insert new employee records and view them, we can now turn your attention on how to update (edit) an existing employee record. A recordset will be created and used to per-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 at one time.
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 per-populated with a record from the database based on an ID value passed from the EMPLOYEE MASTER PAGE (index.php).
Since the primary key value is typically not displayed on the page, its value is "captured" via a hidden field that needs to be created so that the correct ID can be used to help per-populated the fields.
An EDIT link on the EMPLOYEE MASTER PAGE (index.php) that was created earlier will be used to pass data to this edit page to build a new recordset and populate the form. A Search Form will also be created on the search page (search_employee.php) that will be used as well to pass data to this page.
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 eariler.
<h2 class="ui-body ui-body-a ui-corner-all">EDIT EMPLOYEE</h2> <!-- SQL SELECT QUERY -->
<?php
// Store reference of SQL statement in a variable
$query_selectEmployee="SELECT * FROM employees WHERE EmployeeID=3";
$result_selectEmployee=mysql_query($query_selectEmployee);
$row_selectEmployee=mysql_fetch_assoc($result_selectEmployee);
?>
<form action="edit_employee.php" method="post"> <div class="ui-body ui-body-a ui-corner-all"> <p><label for="FirstName">First Name:</label> <input name="FirstName" type="text" autofocus id="FirstName" value="<?php print $row_selectEmployee['FirstName']; ?>" data-mini="true"></p> <p><label for="LastName">Last Name:</label> <input name="LastName" type="text" id="LastName" value="<?php print $row_selectEmployee['LastName']; ?>" data-mini="true"></p> <p><label for="City">City:</label> <input name="City" type="text" id="City" value="<?php print $row_selectEmployee['City']; ?>" data-mini="true"></p> <p><label for="State">State:</label> <input name="State" type="text" id="State" value="<?php print $row_selectEmployee['State']; ?>" data-mini="true"></p> <p><label for="StartDate">Start Date:</label> <input name="StartDate" type="date" id="StartDate" value="<?php print $row_selectEmployee['StartDate']; ?>" data-mini="true"></p> <p><label for="Notes">Notes:</label> <textarea cols="40" rows="8" name="Notes" id="Notes" data-mini="true" class="define-textarea"> <?php print $row_selectEmployee['Note']; ?></textarea></p> <p><label for="Department">Department:</label> <select name="Department" id="Department" data-mini="true" data-inline="true">If you look at the page in Design View in Dreamweaver, you will see the PHP script literally shown INSIDE of the input form elements:Notice you will NOT see a dynamic variable displayed in the screenshot below for the NOTE textarea because it does not have a value attribute like the text input fields.
<option>Human Resource</option>
<option>IT</option>
<option>Engineering</option>
<option>Marketing</option>
<option>Manufacturing</option>
<option>Sales</option>
</select></p> <p><label for="Image">Image:(e.g., john_doe.jpg)</label> <input type="text" name="Image" name="Image" size="20" value="<?php print $row_selectEmployee['Image']; ?>"></p> <p><input type="submit" name="edit_button" value="EDIT EMPLOYEE" data-inline="true" data-mini="true" data-icon="plus" data-iconpos="left"/></p> <input type="hidden" name="submitted" value="true" /><input type="hidden" name="EmployeeID" value="<?php print $row_selectEmployee['EmployeeID']; ?>"/> </div> </form>
// Store a reference of SQL Statement in a variable
$query_selectEmployee = "SELECT * FROM employees WHERE employeeID=$_GET[recordID]";
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 to the database.
// SQL INSERT QUERY --------------------------------------------------------- // Store a reference of SQL statement in a variableTo this:
$query_insertEmployee="INSERT INTO employees(FirstName, LastName, City, State, StartDate, Note, Department, Image, DateEntered) VALUES('$FirstName','$LastName','$City','$State','$StartDate','$Note','$Department','$Image', NOW())"; // Execute query
mysql_query($query_insertEmployee); // Close connection to database mysql_close();
// SQL UPDATE QUERY ----------------------------------------------------- // Store a reference of SQL statement in a variable
$query_updateEmployee="UPDATE employees SET FirstName='$FirstName', LastName='$LastName', City='$City', State='$State', StartDate='$StartDate', Note='$Note', Department='$Department', Image='$Image' WHERE EmployeeID={$_POST['EmployeeID']}";
// $result_updateEmployee=mysql_query($query_updateEmployee);
// Execute query
mysql_query($query_updateEmployee); // Close connection to database
mysql_close();
<input type="hidden" name="submitted" value="true" /> <input type="hidden" name="EmployeeID" value="<?php print $row_selectEmployee['EmployeeID']; ?>"/> </div>
</form>
If you were to click on the EDIT icon link on the Master Page for several of the employees, you will see the the menu on the Edit Employee Page (e.g., edit_employee.php) ALWAYS reflects the first record in the department database table. To SYNC the recordset to the correct value from the employees table requires some additional coding.
<select name="Department" id="Department" data-mini="true" data-inline="true">
<option>Human Resource</option>
<option>IT</option>
<option>Engineering</option>
<option>Marketing</option>
<option>Manufacturing</option>
<option>Sales</option>
</select>
<select name="Department" id="Department" data-mini="true" data-inline="true">
<option>Human Resource</option>
<option>IT</option>
<option selected>Engineering</option>
<option>Marketing</option>
<option>Manufacturing</option>
<option>Sales</option>
</select>
<!-- SQL SELECT QUERY FOR COMBO BOX ----------------------------------------------------> <?php
// Store a reference of SQL statement in a variable
$query_departmentNames="SELECT DepartmentID, Dept FROM departments";
// Store a reference of the query in this variable from the SQL variable using the mysql_query method.
$result_departmentNames=mysql_query($query_departmentNames);
// Store the result of the query into an associative array using the mysql_fetcth_assoc method.
$row_departmentNames=mysql_fetch_assoc($result_departmentNames);
?> <form>
<option>Human Resource</option>To this:
<option>IT</option>
<option>Engineering</option>
<option>Marketing</option>
<option>Manufacturing</option>
<option>Sales</option>
<p><label for="Department">Department:</label>
<select name="Department" id="Department" data-mini="true" data-inline="true"> <?php
do {
if ($row_departmentNames['Dept'] == $row_selectEmployee['Department'])
{
print "<option selected>{$row_departmentNames['Dept']}</option>";
}
else
{
print "<option>{$row_departmentNames['Dept']}</option>";
}
}
while ($row_departmentNames = mysql_fetch_assoc($result_departmentNames));
?> </select></p>
CODE EXPLANATION:
- Within the loop, the "if" statement check each loop iteration to see if the Dept of the first recordset MATCHES the Department of the second recordset and if so add the attribute of "selected" to that <option> tag.
- The "if" statement is used to check if there is an EmployeeID match and if so, it INJECTS the attribute into the code.
- It is important to note that the "if" statement is added before the closing angle bracket of the <option> tag.