User Guide

Table Of Contents
480 Chapter 21: Updating Your Database
2.
Save the page.
3.
View insert_form.cfm in your web browser and enter values.
4.
Click Submit.
ColdFusion inserts your values into the Employee table and displays a confirmation message.
Reviewing the code
The following table describes the highlighted code and its function:
Inserting into specific fields
The preceding example inserts data into all the fields of a table (the Employee table has seven
fields). There might be times when you do not want users to add data into all fields. To insert data
into specific fields, the SQL statement in the
cfquery must specify the field names following
both INSERT INTO and VALUES. For example, the following
cfquery omits salary and start
date information from the update. Database values for these fields are 0 and NULL, respectively,
according to the databases design.
<cfquery name="AddEmployee" datasource="cfdocexamples">
INSERT INTO Employee
(Emp_ID,FirstName,LastName,
Dept_ID,Contract)
VALUES
(#Form.Emp_ID#,'#Form.FirstName#','#Form.LastName#',
#Form.Dept_ID#,'#Form.Contract#')
</cfquery>
Updating data
You usually use the following 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 confirmation message for the
end user.
Code Description
<cfquery name="AddEmployee"
datasource="cfdocexamples">
INSERT INTO Employee
VALUES (#Form.Emp_ID#,
'#Form.FirstName#',
'#Form.LastName#',
#Form.Dept_ID#,
'#Form.StartDate#',
#Form.Salary#,
'#Form.Contract#')
</cfquery>
Inserts a new row into the Employee table of the cfdocexamples
database. Specifies each form field to be added.
Because you are inserting data into all database fields in the
same left-to-right order as in the database, you do not have to
specify the database field names in the query.
Because #From.Emp_ID#, #Form.Dept_ID#, and
#Form.Salary# are numeric, they do not need to be enclosed in
quotation marks.