Creating an Action Page to Insert Data

There are two ways to create an action page to insert data into a database.

The CFINSERT tag is the easiest way to handle simple inserts from either a CFFORM or an HTML form.

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.

Note To create an insert action page with CFINSERT:
  1. Create a new application page in Studio.
  2. Enter the following code:
    <CFINSERT DATASOURCE="CompanyInfo" TABLENAME="Employees">
    
    <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>
    
  3. Save the page. as insertpage.cfm.
  4. View insertform.cfm in a browser, enter values, and click the Submit button.
  5. The data is inserted into the Employees table and the message appears.
Note To create an insert page with CFQUERY:
  1. Create a new application page in Studio.
  2. Enter the following code:
    <CFQUERY NAME="AddEmployee"
    
    DATASOURCE="CompanyInfoB">
    
    INSERT INTO Employees (Fi', '#Form.LastName#',
    
    '#Form.Phone#')
    
    </CFQUERY>
    
    <HTML>
    <HEADER>
        <TITLE>Insert Action Page</TITLE>
    </HEADER>
    
    <BODY>
    <H1>Employee Added</H1>
    <CFOUTPUT>
    You have added #Form.FirstName# #Form.LastName# to the Employees 
    database.
    </CFOUTPUT>
    </BODY>
    </HTML>
    
  3. Save the page. as insertpage.cfm.
  4. View isertform.cfm in a browser, enter values, and click the Submit button.
  5. The data is inserted into the Employees table and the message appears.