| Contents > Getting Started Building ColdFusion MX Applications > Lesson 6: Adding and Updating SQL Data > Completing the Trip Maintenance application > Writing code to save new trips to the database Exercise: insert trip data using SQL INSERT and cfquery |
|
|
|
|
||
In this exercise you will add code to pass the data entered on the Trip Maintenance collection form and insert into the Compass Travel database. To do this, you will be modifying the trip insert action page to use the SQL INSERT statement and the ColdFusion cfquery tag.
<cfif isOk EQ "Yes"> tag near the end of the file. After the <H1>Trip Added
</H1> line, add the following code to insert the data from the Form variables into the Trips table.Tip: To save time, you can copy this code from the tripsinsertquery.txt file (for Windows users) or from tripinsertqueryunix.txt (for UNIX users) in the solutions directory.
For |
Code |
|---|---|
Windows users, using MS Access |
<!--- Insert the new trip record into the Compass
Travel Database --->
<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#')
</cfquery>
|
UNIX users, using PointBase |
<!--- Insert the new trip record into the
Compass Travel Database --->
<!--- Use local variables to convert dates to JDBC format
(yyyy-mm-dd) from input format (mm/dd/yyyy) --->
<cfset JDBCdepartureDate = #Right(Form.departureDate,4)#
& "-" & #Left(Form.departureDate,2)# & "-"
& #Mid(Form.departureDate,4,2)#>
<cfset JDBCreturnDate = #Right(Form.returnDate,4)# & "-"
& #Left(Form.returnDate,2)# & "-"
& #Mid(Form.returnDate,4,2)#>
<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#', Date'#JDBCdepartureDate#',
Date'#JDBCreturnDate#',
#Form.price#,'#Form.tripLeader#', '#Form.photo#',
#Form.baseCost#, #Form.numberPeople#, '#Form.depositRequired#')
</cfquery>
|

After the new trip is written to the database, the following message appears: Trip is added.

The TripResults page appears:

The following page appears:

The following table describes the SQL INSERT and cfquery code used to add data:
Code |
Explanation |
|---|---|
<cfquery name="AddTrip" |
Using the |
INSERT INTO Trips (TripName, |
The SQL INSERT statement identifies that the data are to be inserted into the Trips table. The table column names are cited in a comma-separated list surrounded by parenthesis ( The The values refer to form variables passed from the data entry form to the action page. The variables are surrounded by pound signs; for example, |
For more information about adding data to a database using SQL and cfquery, see Developing ColdFusion MX Applications. For more information about SQL, consult any SQL primer.
|
|
||
| Contents > Getting Started Building ColdFusion MX Applications > Lesson 6: Adding and Updating SQL Data > Completing the Trip Maintenance application > Writing code to save new trips to the database Exercise: insert trip data using SQL INSERT and cfquery |
|
|
ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting
Version 6.1
Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.