To add data using cfinsert:
Open tripeditaction.cfm from the my_app directory in your editor and do the following:
1
Remove the entire AddTrip cfquery that you added in the last exercise (from the beginning
a
<cfquery name ="AddTrip" datasource="CompassTravel">
end tag).
Add the following
b
code that you just deleted:
<cfinsert datasource="CompassTravel" tablename="TRIPS">
Save the page and test it by opening the tripedit.cfm page in your browser.
2
Follow step 4 through step 9 in the previous exercise to verify this approach to inserting new
3
trips.
For more information about adding data to a database using the
ColdFusion MX Applications.
Updating a SQL row using cfupdate
To update an existing SQL row, ColdFusion offers a simple approach for updating SQL rows
through the use of the
attributes to specify where the data is to be inserted. The tag also has a
tablename
attribute to identify which fields are to be inserted.
fields to insert. If this attribute is not specified, all fields in the form are included in the operation.
All the fields of the tripedit.cfm page have corresponding columns in the Trips table, so you can
omit the
FormFields
field is passed from the TripEdit page the cfupdate tag is used otherwise the
executed. The following example uses the
attribute:
<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>
Reviewing the code
The following tables describes the
Code
<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>
124
Chapter 10: Lesson 6: Adding and Updating SQL Data
tag to insert data into the trips table in the same location as the
cfinsert
tag. Like
cfupdate
cfinsert
attribute for both the
cfupdate
cfinsert
cfinsert
, the
cfupdate
is a comma-separated list of form
Formfields
and
cfinsert
cfupdate
and
cfinsert
and
code:
cfupdate
Explanation
The ColdFusion function
determines whether the hidden field tripID was
passed to the action page from tripedit.cfm. If
there is a current trip, the
returns True. When there is no current trip, the
statement is True. When the
cfif
statement is True, the
and the main page displays with the updated
trip. If the
statement evaluates to False,
cfif
the
statement executes and the first
cfinsert
trip displays in the main page.
tag to the
</cfquery>
tag, see Developing
tag has
and
datasource
formfields
tags. If the
form
tripID
tag is
cfinsert
without the
FormFields
IsDefined
function
isDefined
cfif
tag executes
cfinsert
Need help?
Do you have a question about the COLDFUSION MX 61-GETTING STARTED BUILDING COLDFUSION MX and is the answer not in the manual?
Questions and answers