Inserting Data
Creating an insert action page with cfquery
For more complex inserts from a form submittal you can use a SQL INSERT
statement in a
more flexible because you can insert information selectively or use functions within
the statement.
To create an insert page with cfquery:
1
2
3
4
5
6
tag instead of a
cfquery
Rename (or delete) the insertaction.cfm page that you created in the previous
section.
Create a new application page in ColdFusion Studio.
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 --->
<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>
<html>
<head>
<title>input form</title>
</head>
<body>
<h1>Employee Added</h1>
<cfoutput>You have added #Form.FirstName# #Form.Lastname# to the
employees database.
</cfoutput>
</body>
</html>
Save the page as insertaction.cfm.
View insertform.cfm in a browser, enter values, and click Submit.
The data is inserted into the Employee table and the message displays.
tag. The SQL INSERT statement is
cfinsert
105
Need help?
Do you have a question about the COLDFUSION 5-DEVELOPING and is the answer not in the manual?