User Guide

Inserting Data 103
3 Save the file as insertform.cfm in the myapps directory.
4 View insertform.cfm in a browser.
Data entry form notes and considerations
If you use the cfinsert tag in the action page to insert the data into the database,
you should follow these rules for creating the form page:
You only need to create HTML form fields for the database fields into which you
want to insert data.
By default,
cfinsert inserts all of the forms fields into the database table fields
with the same names. For example, it puts the Form.Emp_ID value in the
database Emp_ID field. The tag ignores any form fields with no corresponding
database column name. You can also use the
formfields attribute to specify the
fields you want to insert.
Creating an action page to insert data
You can use the cfinsert tag or cfquery tag to create an action page that inserts
data into a 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 new application page in ColdFusion Studio.
2 Enter the following code:
<!--- Make the contract variable be No if it is not set (check box is
empty) --->
<cfif not isdefined("Form.Contract")>
<cfset Form.contract = "No">
</cfif>
<!--- Insert the new record --->
<cfinsert datasource="CompanyInfo" tablename="Employee">
<html>
<head>
<title>input form</title>
</head>
<body>
<h1>Employee Added</h1>
<cfoutput>You have added #Form.FirstName# #Form.Lastname# to the
employees database.