In , you created the tripeditaction.cfm page to contain server side edits for the trip edit data entry form. In this final lesson, you will complete the tripeditaction.cfm page. To complete the action page, you will write code to do these tasks:
cfinsert
tag.cfupdate
tag.In addition to completing the Trip Maintenance application, you will develop a ColdFusion page to update all the prices in the database using the cfquery tag and the SQL UPDATE statement.
In , you built a Trip Edit page to collect the data. Now you can modify the Trip Edit action page to insert the data into the database. There are two approaches to inserting data into a SQL database:
cfquery
tag.
cfinsert
tag. This approach eliminates the need for you to learn SQL syntax.In previous lessons, you used the SQL SELECT statement to retrieve data and the SQL DELETE statement to delete data from the Trips table in the Compass Travel database. To add new trips to the database using SQL, you must understand the syntax of the SQL INSERT statement.
The SQL INSERT statement inserts new rows into a relational table. The format of the INSERT statement is as follows:
INSERT INTO table_name
VALUES (value1, value2,....)
The database table named Clients contains information about people in the following rows:
LastName |
FirstName |
Address |
City |
---|---|---|---|
Tom |
Jones |
12 State St |
Boston |
Peter |
Green |
1 Broadway |
New York |
After the following SQL statement executes:
INSERT INTO Clients
VALUES ('Smith', 'Kaleigh', '14 Greenway', 'Windham')
the table contains the following rows:
LastName |
FirstName |
Address |
City |
---|---|---|---|
Tom |
Jones |
12 State St |
Boston |
Peter |
Green |
1 Broadway |
New York |
Smith |
Kaleigh |
14 Greenway |
Windham |
Notice that the values inserted in the table were surrounded by single quotation marks. In SQL, you must surround any text or date values with single quotation marks but numeric values are not.
Alternatively, you can specify the columns for which you want to insert data. This approach lets you insert data to some columns while omitting others. The syntax for this approach is as follows:
INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)
For example, the syntax to add Kaleigh Smith of Windham, with the address unknown, you use the named column approach:
INSERT INTO Clients (LastName, FirstName, City)
VALUES ('Smith', 'Kaleigh', 'Windham')
You used the cfquery
tag to execute SQL from ColdFusion. The cfquery
tag passes SQL statements to your data source. As described in Chapter 4, a data source stores information about how to connect to an indicated data provider, such as a relational database management system. The data source you established in Chapter 4 stored information on how to access the Compass Travel database. The data source name was called "CompassTravel".
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.
tripedit.cfm
in your browser.
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:
For more information about adding data to a database using SQL and cfquery
, see Developing ColdFusion MX Applications with CFML. For more information about SQL, consult any SQL primer.
For those who would 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 cfinsert
tag. As you might expect, the cfinsert
tag has datasource
and tablename
attributes to specify where the data is inserted. The tag also has a formfields
attribute to identify which fields to insert. Formfields
is a comma-separated list of form fields to insert. If this attribute is not specified, all fields in the form are included in the operation. The following example uses the cfinsert
with these attributes:
<cfinsert datasource="CompassTravel" tablename="Trips"
formfields="tripName, eventType, tripDescription, tripLocation, departureDate, returnDate, price, tripLeader, photo, baseCost, numberPeople, depositRequired">
The cfinsert
tag used in the previous code snippet uses the following attributes:
In this exercise, you change the approach the action page uses to insert the data into the database. You will replace the SQL INSERT statement with the cfinsert
tag.
<cfquery name ="AddTrip" datasource="CompassTravel">
tag to the </cfquery>
end tag).
cfinsert
tag to insert data into the trips table in the same location as the code that you just deleted:<cfinsert datasource="CompassTravel" tablename="TRIPS">
For more information about adding data to a database using the cfinsert
tag, see Developing ColdFusion MX Applications with CFML.
To update an existing SQL row, ColdFusion offers a simple approach for updating SQL rows through the use of the cfupdate
tag. Like cfinsert
, the cfupdate
tag has datasource
and tablename
attributes to specify where the data is to be inserted. The tag also has a formfields
attribute to identify which fields are to be inserted. Formfields
is a comma-separated list of form 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
attribute for both the cfinsert
and cfupdate
tags. If the tripID
form field is passed from the TripEdit page the cfupdate tag is used otherwise the cfinsert
tag is executed. The following example uses the cfupdate
and cfinsert
without the FormFields
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>
The following tables describes the cfinsert
and cfupdate
code:
In this exercise, you will add the code to update the trip data into the database. You will add the cfupdate
tag to the tripeditaction.cfm page.
For example, depending on your web server configuration, the photolocation path might be:
<cfset PhotoLocation
"C:\cfusionmx\wwwroot\CFDOCS\getting_started\Photos\">
or
<cfset PhotoLocation =
"C:\Inetpub\wwwroot\CFDOCS\getting_started\Photos\">
<cfset PhotoLocation = "/opt/coldfusionmx/wwwroot/cfdocs/
getting_started/photos/">
or
<cfset PhotoLocation = "/<webserverdocroot>/cfdocs/
getting_started/photos/">
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.
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 = ''> <cfset eventTypeIdentifier = #TripQuery.eventType#> <cfset tripLocation = ''> <cfset departureDate = ''> <cfset returnDate = ''> <cfset price = ''> <cfset tripLeader = ''> <cfset photo = ''> <cfset baseCost = ''> <cfset numberPeople = ''> <cfset depositRequired = ''>
</cfif>
The following table describes the code used to properly initialize the trip edit form:
In this exercise you will link the Add and Edit buttons on the Trip Detail page with the Trip Edit page.
</cfif>
tag at the end of the file.
<!--- EDIT BUTTON ---> <cfelseif IsDefined("Form.btnEdit")> <cflocation url="tripedit.cfm?ID=#Form.RecordID#"> <!--- ADD BUTTON ---> <cfelseif IsDefined("Form.btnAdd")> <cflocation url="tripedit.cfm"> </cfif>
<HTML>
tag in the tripedit4.cfm page.
The ColdFusion cfupdate
works well for updating a single record. To update several records in a single query, you must use the SQL UPDATE statement in conjuction with cfquery
.
The SQL UPDATE statement updates or changes rows in a relational table. The syntax of the UPDATE statement is as follows:
UPDATE table_name SET column_name = new_value
WHERE column_name = some_value
Consider a database table named Clients that contains information about people in the following rows:
PersonID |
LastName |
FirstName |
Age |
---|---|---|---|
1 |
Green |
Tom |
12 |
2 |
Wall |
Peter |
42 |
3 |
Madigan |
Jess |
20 |
After the following SQL statement executes:
UPDATE Clients SET LastName = 'Pitt'
WHERE ID = 3
the table contains the following rows:
PersonID |
LastName |
FirstName |
Age |
---|---|---|---|
1 |
Green |
Tom |
12 |
2 |
Wall |
Peter |
42 |
3 |
Pitt |
Jess |
20 |
The UPDATE statement updates all rows that meet the criteria found in the WHERE clause. If there is no WHERE clause, every row of the table is updated. After the following SQL statement executes:
UPDATE Clients SET Age = Age + 1
WHERE ID = 3
the table contains the following rows:
PersonID |
LastName |
FirstName |
Age |
---|---|---|---|
1 |
Tom |
Green |
12 |
2 |
Peter |
Green |
42 |
3 |
Pitt |
Jess |
21 |
The cfupdate
statement works well when you want to update the current record within a cfquery
. Alternatively, you can update several rows within a table by issuing a single query using cfquery
and the SQL UPDATE statement. For example, if the base cost of all trips increased by 5%, you could issue the following query:
<!-- Routine to increase trip base Cost by 5% -->
<cfquery name="TripQuery" dataSource="CompassTravel"> UPDATE Trips SET baseCost = baseCost * 1.05
</cfquery>
In this exercise, you will develop a page to increase the price of every trip by 10%. This page runs only once and is not part of the Trips Maintenance application. This exercise shows how to update many database rows using a single SQL UPDATE statement and the ColdFusion cfquery
tag.
<!---Routine to increase trip price by 10% ---> <cfquery name="TripQuery" dataSource="CompassTravel"> UPDATE trips SET price = price * 1.1 </cfquery> <cfoutput> New prices are now in effect.</cfoutput>
In this lesson you used the cfinsert
and cfupdate
tags to add and update data to a SQL table. You also have used the SQL UPDATE statement in conjuction with the cfquery
tag to effect a trip price increase for all rows in the Trips table.
You have completed the Getting Started tutorial. You should understand how you can combine CFML and SQL to develop powerful applications. When compared with traditional development methods, ColdFusion helps speed the development in hand crafting a database solution like the one in this tutorial. Remarkably, however, depending on the editor or IDE that you use to develop applications, much of the work in this tutorial can be autogenerated using built-in wizards, which simplifies the development process even more.