
424
DEVELOPING
COLDFUSION 9 APPLICATIONS
Accessing and Using Data
Las
t
upda
te
d 8/5/2010
3
View update_form.cfm in your web browser by specifying the page URL and an Employee ID; for example, enter
the following:
http://localhost/myapps/update_form.cfm?Emp_ID=3
Note:
Although you can view an employee’s information, code an action page before you can update the database. For
more information, see “
Creating an action page to update data
” on page
424.
Reviewing the code
The following table describes the code and its function:
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.
Code
Description
<cfquery name="GetRecordtoUpdate"
datasource="cfdocexamples">
SELECT * FROM Employee
WHERE Emp_ID = #URL.Emp_ID#
</cfquery>
Queries the cfdocexamples data source and returns records in which
the employee ID matches what was entered in the URL that called this
page.
<cfoutput query="GetRecordtoUpdate">
...
</cfoutput>
Makes available as variables the results of the GetRecordtoUpdate
query in the form created in subsequent lines.
<form action="update_action.cfm" method="Post">
...
</form>
Creates a form whose variables are processed on the
update_action.cfm action page.
<input type="Hidden" name="Emp_ID"
value="#Emp_ID#"><br>
Uses a hidden input field to pass the Emp_ID (primary key) value to
the action page.
First Name:
<input type="text" name="FirstName"
value="#FirstName#"><br>
Last Name:
<input type="text" name="LastName"
value="#LastName#"><br>
Department Number:
<input type="text" name="Dept_ID"
value="#Dept_ID#"><br>
Start Date:
<input type="text" name="StartDate"
value="#StartDate#"><br>
Salary:
<input type="text" name="Salary"
value="#Salary#"><br>
Populates the fields of the update form. This example does not use
ColdFusion formatting functions. As a result, start dates look like
1985-03-12 00:00:00 and salaries do not have dollar signs or commas.
The user can replace the information in any field using any valid input
format for the data.
Contracto r:
< cfif #Contract# IS "Yes">
<input type="checkbox" name="C ontract"
checked>Yes<br>
<cfelse>
<input type="checkbox" name="Contract"> Yes <br>
</cfif>
<br>
<input type="Submit" value="Update Information">
</form>
</cfoutput>
The Contract field requires special treatment because a check box
appears and sets its value. The cfif structure puts a check mark in the
check box if the Contract field value is
Yes
, and leaves the box empty
otherwise.
Содержание COLDFUSION 9
Страница 1: ...Developing Applications ADOBE COLDFUSION 9...