# Creating an Action Page to Update Data

You can create an action page to update data with either the CFUPDATE tag or CFQUERY with the UPDATE statement.

The CFUPDATE tag is the easiest way to handle simple updates from a front end form. The CFUPDATE tag has an almost identical syntax to the CFINSERT tag.

To use CFUPDATE, you must include all of the fields that make up the primary key in your form submittal. The CFUPDATE tag automatically detects the primary key fields in the table you are updating and looks for them in the submitted form fields. ColdFusion uses the primary key field(s) to select the record to update. It then updates the appropriate fields in the record using the remaining form fields submitted.

For more complicated updates, you can use a SQL UPDATE statement in a CFQUERY tag instead of a CFUPDATE tag. The SQL update statement is more flexible for complicated updates.

| To create an update page with CFUPDATE:

1. Create a new application page in Studio.
2. Enter the following code:

       <CFUPDATE DATASOURCE="CompanyInfo"

           TABLENAME="Employees">

       <HTML>
       <HEAD>
           <TITLE>Update Employee</TITLE>
       </HEAD>
       <BODY>

       <H1>Employee Added</H1>
       <CFOUTPUT>
       You have updated the information for #Form.FirstName# #Form.LastName#
       in the Employees database.
       </CFOUTPUT>

       </BODY>
       </HTML>

3. Save the page. as `updatepage.cfm`.
4. View `updateform.cfm` in a browser, enter values, and click the Submit button.
5. The data is updated in the Employees table and the message appears.

| To create an update page with CFQUERY:

1. Create a new application page in Studio.
2. Enter the following code:

       <CFQUERY NAME="UpdateEmployee"

       DATASOURCE="CompanyInfo">

       UPDATE Employees

       SET Firstname='#Form.Firstname#',

       LastName='#Form.LastName#',

       Department_ID='#Form.Department_ID#'

       StartDate='#Form.StartDate#'>

       Salary=#Form.Salary#>

           WHERE Employee_ID=#Employee_ID#
       </CFQUERY>

       <H1>Employee Added</H1>
       <CFOUTPUT>
       You have updated the information for #Form.FirstName# #Form.LastName#
       in the Employees database.
       </CFOUTPUT>

3. Save the page. as `updatepage.cfm`.
4. View `updateform.cfm` in a browser, enter values, and click the Submit button.
5. The data is updated into the Employees table and the message appears.

### Code Review

Code | Description
--- | ---
    <CFQUERY NAME="UpdateEmployee"
        DATASOURCE="CompanyInfo">
        UPDATE Employees
            SET Firstname='#Form.Firstname#',
            LastName='#Form.LastName#',

    Department_ID='#Form.Department_ID#'
            StartDate='#Form.StartDate#'>
            Salary=#Form.Salary#>
        WHERE Employee_ID=#Employee_ID#
    </CFQUERY>
| After the SET clause, you must name a table column. Then, you indicate a constant or expression as the value for the column. Be sure to remember the WHERE statement. If you do not use it, If you do not use it, the SQL UPDATE statement will apply the new information to every row in the database.