User Guide

Updating Data 109
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.
Creating an update action page with cfupdate
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 the field or fields that make up the primary key in
your form submittal. The
cfupdate tag automatically detects the primary key fields
in the table that you are updating and looks for them in the submitted form fields.
ColdFusion uses the primary key fields to select the record to update. (Therefore, you
cannot update the primary key value itself.) It then uses the remaining form fields
that are submitted to update the corresponding fields in the record. Your form only
needs to have fields for the database fields that you want to change.
To create an update page with cfupdate:
1 Create a new application page in ColdFusion Studio.
2 Enter the following code:
<cfif not isdefined("Form.Contract")>
<cfset form.contract = "No">
<cfelse>
<cfset form.contract = "Yes">
</cfif>
<cfupdate datasource="CompanyInfo"
tablename="Employee">
<html>
<head>
<title>Update Employee</title>
</head>
<body>
<h1>Employee Updated</h1>
<cfoutput>
You have updated the information for #Form.FirstName#
#Form.LastName# in the Employees database.
</cfoutput>
</body>
</html>
3 Save the page. as updateaction.cfm.
4View
updateform.cfm in a browser by specifying the page URL and an Employee
ID, for example,
http://localhost/myapps/updateform.cfm?Emp_ID=3. Enter
new values in any of the fields, and click the Submit button.
5 The data is updated in the Employee table and the message appears.