Programming instructions
138 Lesson 6 Adding and Updating SQL Data
For more information about adding data to a database using the cfupdate tag, see
Developing ColdFusion MX Applications with CFML.
Now that you have built the data entry form adding new and updating existing trips, you
will add the logic to link it to the main trip page to test the update logic.
Linking the Trip Edit page to the main page
As discussed in Lesson 4, 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 the next exercise you will insert the code to call tripedit.cfm from maintenance.cfm as
follows:
<!--- EDIT BUTTON --->
<cfelseif IsDefined("Form.btnEdit")>
<cflocation url="tripedit.cfm?ID=#Form.RecordID#">
<!--- ADD BUTTON --->
<cfelseif IsDefined("Form.btnAdd")>
<cflocation url="tripedit.cfm">
</cfif>
Notice that when the user clicks the Add button, the maintenanceaction.cfm navigates to
tripedit.cfm passing no arguments. Conversely, when the user clicks the Edit button, the
Trip Edit page passes the current record id. The Trip Edit page must handle both cases.
When a
RecordID is passed on the URL, tripedit.cfm must query the database and fill
the form with the data for the corresponding trip. The following code properly initializes
the trip edit form:
<cfif IsDefined("URL.ID")>
<cfquery name="TripQuery" datasource="CompassTravel" maxrows="1">
SELECT tripName, eventType, tripDescription, tripLocation,
departureDate, returnDate, price, tripLeader, photo, baseCost,
numberPeople, depositRequired, tripID
FROM trips
<cfif IsDefined("URL.ID")>
WHERE tripID = #ID#
</cfif>
</cfquery>
<!-- Set the local variables -->
<cfset tripName = '#TripQuery.tripName#'>
<cfset eventType = #TripQuery.eventType#>
<cfset tripDescription = '#TripQuery.tripDescription#'>
<cfset tripLocation = '#TripQuery.tripLocation#'>
<cfset departureDate = DateFormat(#TripQuery.departureDate#,"mm/dd/yyyy")>
<cfset returnDate = DateFormat(#TripQuery.returnDate#,"mm/dd/yyyy")>
<cfset price = #TripQuery.price#>
<cfset tripLeader = '#TripQuery.tripLeader#'>
<cfset photo = '#TripQuery.photo#'>
<cfset baseCost = #TripQuery.baseCost#>
<cfset numberPeople = #TripQuery.numberPeople#>
<cfset depositRequired = '#TripQuery.depositRequired#'>
<cfelse>
<cfset tripName = ''>
<cfset eventType = ''>
<cfset tripDescription = ''>