
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
.
4
View
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.
Содержание COLDFUSION 5-DEVELOPING
Страница 1: ...Macromedia Incorporated Developing ColdFusion Applications MacroMedia ColdFusion 5 ...
Страница 58: ...38 Chapter 3 Querying a Database ...
Страница 134: ...114 Chapter 7 Updating Your Database ...
Страница 210: ...190 Chapter 10 Reusing Code ...
Страница 232: ...212 Chapter 11 Preventing and Handling Errors ...
Страница 238: ...218 Chapter 12 Using the Application Framework ...
Страница 262: ...242 Chapter 12 Using the Application Framework ...
Страница 278: ...258 Chapter 13 Extending ColdFusion Pages with CFML Scripting ...
Страница 320: ...300 Chapter 15 Indexing and Searching Data ...
Страница 336: ...316 Chapter 16 Sending and Receiving E mail ...
Страница 374: ...354 Chapter 18 Interacting with Remote Servers ...