User Guide

106 Chapter 7 Updating Your Database
Reviewing the code
The following table describes the highlighted code and its function:
Updating Data
You usually use two application pages to update data in a database:
An update form
An update action page
You can create an update form with
cfform tags or HTML form tags. The update
form calls an update action page, which can contain either a
cfupdate tag or a
cfquery tag with a SQL UPDATE statement. The update action page should also
contain a message for the end user that reports on the update completion.
Creating an update form
An update form is similar to an insert form, but there are two key differences:
An update form contains a reference to the primary key of the record that is being
updated.
A primary key is a field or combination of fields in a database table that uniquely
identifies each record in the table. For example, in a table of employee names
and addresses, only the Emp_ID would be unique to each record.
An update form is usually populated with existing record data because the forms
purpose is to update data.
The easiest way to designate the primary key in an update form is to include a hidden
input field with the value of the primary key for the record you want to update. The
hidden field indicates to ColdFusion which record to update.
Code Description
<cfquery name="AddEmployee"
datasource="CompanyInfo">
INSERT INTO Employee
VALUES (#Form.Emp_ID#,
#Form.FirstName#,
#Form.LastName#,
#Form.Dept_ID#,
#Form.StartDate#,
#Form.Salary#,
#Form.Contract#)
</cfquery>
Use a cfquery tag to insert a new row
into the Employee table of the
CompanyInfo Database. Specify each
form field to be added. Because the form
and database field names are identical,
you do not have to specify the database
field names in the query.