138
Chapter 12: Lesson 9: Enabling Database Maintenance
Reviewing the code
The following tables describes the
cfinsert
and
cfupdate
code:
To update the database using a cfupdate tag:
1.
Open the tripeditaction.cfm file in the my_app directory in your editor.
2.
Add the following code before the
</body>
tag at the end of the file:
<!--- Update the database --->
<cfif isOk EQ "Yes">
<cfif isdefined("form.tripID")>
<cfupdate datasource="CompassTravel" tablename="trips">
<cflocation url="tripdetail.cfm?ID=#Form.tripID#">
<cfelse>
<cfinsert datasource="CompassTravel" tablename="TRIPS">
<cflocation url="tripdetail.cfm">
</cfif>
<cfoutput>You have added #Form.TripName# to the trips database.
</cfoutput>
</cfif>
3.
Save the file.
For more information about adding data to a database using the
cfupdate
tag, see
ColdFusion
MX Developer’s Guide
.
Exercise 5: Linking the Trip Edit page to the main page
As discussed in
“Lesson 7: Validating Data to Enforce Business Rules” on page 103
, the action
page for the maintenance buttons on the main page is maintenanceaction.cfm
.
You previously
added code for the Search and Delete buttons. In this exercise, you add code for the Add and Edit
buttons.
To link the Add and Edit buttons on the Trip Detail page:
1.
Open the maintenanceaction.cfm file in the my_app directory in your editor.
2.
Locate the
</cfif>
tag at the end of the file.
Code Explanation
<cfif not isdefined("form.tripID")>
<cfinsert datasource="CompassTravel"
tablename="Trips">
<cflocation url="tripdetail.cfm">
<cfelse>
<cfupdate datasource="CompassTravel"
tablename="Trips">
<cflocation
url="tripdetail.cfm?ID=#Form.tripID#">
</cfif>
The
IsDefined
function determines whether
the hidden field
tripID
was passed to the
action page from the tripedit.cfm page. If there
is a current trip, the
IsDefined
function returns
True. When there is no current trip, the
cfif
statement is True. When the
cfif
statement is
True, the
cfinsert
tag executes and the main
page appears with the updated trip. If the
cfif
statement evaluates to False, the
cfinsert
statement executes and the first trip appears in
the main page.
Summary of Contents for COLFUSION MX 7-GETTING STARTED BUILDING COLDFUSION...
Page 1: ...COLDFUSION MX7 Getting Started Building ColdFusion MX Applications...
Page 6: ...6 Contents...
Page 10: ......
Page 14: ...14 Chapter 1 Introducing ColdFusion MX...
Page 38: ...38 Chapter 3 Database Fundamentals...
Page 40: ......
Page 58: ...58 Chapter 5 Lesson 2 Configuring Your Development Environment...
Page 70: ...70 Chapter 6 Lesson 3 Retrieving Data...
Page 84: ...84 Chapter 7 Lesson 4 Building Dynamic Queries...
Page 96: ...96 Chapter 8 Lesson 5 Creating a Trip Detail Page...
Page 102: ...102 Chapter 9 Lesson 6 Creating a Main Application Page...