System information

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.