Exercise 3: Adding Data Using The Cfinsert Tag - MACROMEDIA COLFUSION MX 7-GETTING STARTED BUILDING COLDFUSION MX Getting Started

Building coldfusion mx applications
Table of Contents

Advertisement

Reviewing the code
The following table describes the SQL INSERT and
data:
Code
<cfquery name="AddTrip"
datasource="CompassTravel">
INSERT INTO Trips (TripName,
EventType, tripDescription,
tripLocation, departureDate,
returnDate, price,
tripLeader,photo,
baseCost, numberPeople,
depositRequired)
VALUES ( '#Form.TripName#',
#Form.EventType#,
'#Form.tripDescription#',
'#Form.tripLocation#',
'#Form.departureDate#',
'#Form.returnDate#',
#Form.price#,
'#Form.tripLeader#',
'#Form.photo#',
#Form.baseCost#,
Form.numberPeople#,
'#Form.depositRequired#)
For more information about adding data to a database using SQL and the
ColdFusion MX Developer's Guide. For more information about SQL, consult any SQL primer.

Exercise 3: Adding data using the cfinsert tag

For developers who prefer not to have to remember SQL syntax to add information to SQL
databases, ColdFusion simplifies the coding for inserting SQL rows through the use of the
tag. As you might expect, the
cfinsert
to specify where the data is inserted. The tag also has a
fields to insert. The
If this attribute is not specified, all the fields in the form are included in the operation. The
following example uses the
<cfinsert datasource="CompassTravel" tablename="Trips"
formfields="tripName, eventType, tripDescription, tripLocation,
departureDate, returnDate, price, tripLeader, photo, baseCost,
numberPeople, depositRequired">
The
tag used in the preceding code snippet uses the following attributes:
cfinsert
Attribute
datasource
tablename
formfields
Explanation
Using the
CompassTravel data source and returns a result set identified by
the
The SQL INSERT statement identifies the data to insert into the
Trips table. The table column names are cited in a comma-
separated list surrounded by parentheses
EventType....)
The
into the columns in the same order as the columns are specified
earlier in the statement.
The values refer to form variables that are passed from the data
entry form to the action page. The variables are surrounded by
number signs; for example,
data type is a String data type, the values are surrounded by
single-quotation marks; for example:
attribute specifies a comma-separated list of form fields to insert.
formfields
tag with these attributes:
cfinsert
Description
The data source name associated with the database where the data is inserted.
The name of the SQL table within the database where the data is inserted.
A comma-separated list of form fields to insert.
cfquery
attribute, the
datasource
attribute.
name
after the table name Trips.
keyword indicates the list of values that are inserted
VALUES
tag has
cfinsert
datasource
formfields

Exercise 3: Adding data using the cfinsert tag

code that ColdFusion uses to add
tag connects to the
cfquery
(TripName,
. Also, if the column
#Form.baseCost#
'#Form.TripName#'
cfquery
and
tablename
attribute to identify which
.
tag, see
attributes
135

Advertisement

Table of Contents
loading

This manual is also suitable for:

Colfusion mx 7 - installing and using coldfusion mx

Table of Contents