User Guide

Table Of Contents
478 Chapter 21: Updating Your Database
Creating an insert action page with cfinsert
The
cfinsert tag is the easiest way to handle simple inserts from either a cfform or an HTML
form. This tag inserts data from all the form fields with names that match database field names.
To create an insert action page with cfinsert:
1.
Create a ColdFusion page with the following content:
<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 --->
<cfinsert datasource="cfdocexamples" tablename="Employee">
<h1>Employee Added</h1>
<cfoutput>You have added #Form.FirstName# #Form.Lastname# to the
employee database.
</cfoutput>
</body>
</html>
2.
Save the page as insert_action.cfm.
3.
View insert_form.cfm in your web browser and enter values.
Note: You might want to compare views of the Employee table in the cfdocexamples data source
before and after inserting values in the form.
4.
Click Submit.
ColdFusion inserts your values into the Employee table and displays a confirmation message.
Reviewing the code
The following table describes the code and its function:
Code Description
<cfif not isdefined("Form.Contract")>
<cfset Form.Contract = "No">
</cfif>
Sets the value of Form.Contract to No if it is not
defined. If the Contractor check box is unchecked,
no value is passed to the action page; however, the
database field must have some value.