Creating Update Action Page

The action page that you build to update data is very similar to the action page that you build to insert data. The update action page needs to include:

Building a query to update data

The CFQUERY tag supports a number of SQL statements that you use to insert, update, and delete records in a table.

For example, you'll use the UPDATE SQL statement, and SET and WHERE clauses to describe:

SQL UPDATE syntax

<CFQUERY NAME="QueryName" DATASOURCE="DataSourceName">
    UPDATE Table
    SET
    ColumnName='UpdateValue',
    ColumnName='UpdateValue',
    ...
    WHERE PrimaryKey=UpdateValue
</CFQUERY>
Note Note:The purpose of this guide is to get you up and running with ColdFusion Express. Allaire suggests that you purchase a book on SQL to learn how to use it efficiently.

Updating data usage example

This code updates a particular employee in the Employees table:

<CFQUERY NAME="UpdateEmployee" DATASOURCE="HRApp">
    UPDATE Employees
    SET
    FirstName='#Form.FirstName#',
    LastName='#Form.LastName#',
    Contract='#Form.Contract#'
    WHERE 
    Employee_ID=#Form.Employee_ID#
</CFQUERY>
Note To build an update action page:
  1. Create a new application page in HomeSite.
  2. Save the page as UpdateAction.cfm.
  3. Title the page Chapter 10 - Update Employee Action Page.
  4. Set a local variable to yes if the Form.Contract variable was passed from UpdateForm.cfm and NO if it was not passed:
  5. <CFIF IsDefined("Form.Contract") IS "Yes">
        <CFSET ContractStatus="Yes">
    <CFELSE>
        <CFSET ContractStatus="No">
    </CFIF>
    
  6. Create a query named UpdateEmployee to update data in the HRExpress database:
  7. <CFQUERY NAME="UpdateEmployee" DATASOURCE="HRExpress">
    </CFQUERY>
    
  8. Within the CFQUERY block, create a SQL statement to update data in the Employees table using the information passed from UpdateForm.cfm:
  9. UPDATE Employees
    SET  FirstName = '#Form.FirstName#',LastName = '#Form.LastName#', 
    Department_ID = #Form.Department_ID#,
    StartDate = #Form.StartDate#, Salary = #Form.Salary#,
    Contract = '#ContractStatus#'
    WHERE   Employee_ID = #Form.Employee_ID#
    
  10. After the CFQUERY block, redirect users to EmpList.cfm.
  11. <CFLOCATION URL="EmpList.cfm">
    
  12. Save the page.
  13. View UpdateList.cfm in a browser.
  14. Select an employee to update.
  15. Change information and submit the form.
  16. UpdateAction.cfm updates the information in the database and redirects users to EmpList.cfm so that they can confirm the changes.

Click here to see how the update list, form and action page should work.

Click here to see UpdateAction.cfm's code.

You have completed the HR Manager Application. Move on to the chapter summary.