User Guide

Table Of Contents
Inserting data 479
Note: If you use form variables in cfinsert or cfupdate tags, ColdFusion automatically validates any
form data it sends to numeric, date, or time database columns. You can use the hidden field validation
functions for these fields to display a custom error message. For more information, see Chapter 26,
“Introduction to Retrieving and Formatting Data,” on page 609.
Creating an insert action page with cfquery
For more complex inserts from a form submittal, you can use a SQL INSERT statement in a
cfquery tag instead of using a cfinsert tag. The SQL INSERT statement is more flexible
because you can insert information selectively or use functions within the statement.
The following procedure assumes that you have created the insert_action.cfm page, as described
in “Creating an insert action page with cfinsert” on page 478.
To create an insert action page with cfquery:
1.
In insert_action.cfm, replace the cfinsert tag with the following highlighted cfquery code:
<html>
<head>
<title>Input form</title>
</head>
<body>
<!--- If the Contractor check box is clear),
set the value of the Form.Contract to "No" --->
<cfif not isdefined("Form.Contract")>
<cfset Form.Contract = "No">
</cfif>
<!--- Insert the new record --->
<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>
<h1>Employee Added</h1>
<cfoutput>You have added #Form.FirstName# #Form.Lastname# to the
employee database.
</cfoutput>
</body>
</html>
<cfinsert datasource="cfdocexamples"
tablename="Employee">
Creates a new row in the Employee table of the
cfdocexamples database. Inserts data from the form
into the database fields with the same names as the
form fields.
<cfoutput>You have added
#Form.FirstName# #Form.Lastname#
to the employee database.
</cfoutput>
Informs the user that values were inserted into the
database.
Code Description