You usually use two application pages to insert data into a database:
You can create an insert form with standard HTML form tags or with cfform
tags (see "Creating Forms with the cfform Tag"). When the user submits the form, form variables are passed to a ColdFusion action page that performs an insert operation (and whatever else is called for) on the specified data source. The insert action page can contain either a cfinsert
tag or a cfquery
tag with a SQL INSERT statement. The insert action page should also contain a message for the end user.
The following procedure creates a form using standard HTML tags.
<html> <head> <title>Insert Data Form</title> </head> <body> <H2>Insert Data Form</H2> <form action="insertaction.cfm" method="post"> Employee ID: <input type="text" name="Emp_ID" size="4" maxlength="4"><br> First Name: <input type="Text" name="FirstName" size="35" maxlength="50"><br> Last Name: <input type="Text" name="LastName" size="35" maxlength="50"><br> Department Number: <input type="Text" name="Dept_ID" size="4" maxlength="4"><br> Start Date: <input type="Text" name="StartDate" size="16" maxlength="16"><br> Salary: <input type="Text" name="Salary" size="10" maxlength="10"><br> Contractor: <input type="checkbox" name="Contract" value="Yes" checked>Yes<br> <br> <input type="Reset" value="Clear Form"> <!-- Submit button --> <input type="Submit" value="Submit"> </form> </body> </html>
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:
cfinsert
inserts all of the form's 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.
You can use the cfinsert
tag or cfquery
tag to create an action page that inserts data into a database.
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.
<!--- 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. </cfoutput> </body> </html>
The following table describes the code and its function:
Note If you use form variables in |
For more complex inserts from a form submittal you can use a SQL INSERT statement in a cfquery
tag instead of a cfinsert
tag. The SQL INSERT statement is more flexible because you can insert information selectively or use functions within the statement.
<!--- 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>
The following table describes the highlighted code and its function: