Inserting Records

Inserting a new record into a database table is a two-step process. The first step involves creating a template with an input form that collects the information you want to insert. The second step in the process takes the form-field data and inserts it into the database table. The code for this process can be broken up into two templates, but it is possible to use a single template (with conditional code, as described in Chapter 3) that posts to itself if you desire. With the introduction of ColdFusion Components (CFCs) in ColdFusion MX, several more advanced options exist for architecting this type of functionality. CFCs are covered in Chapter 22. For now, we’ll focus on the first two techniques,

Inputting Data Via Forms

The most popular method for collecting data to insert into a database is via an HTML form. When creating your input form, you should name your form fields the same as their equivalent database fields (or stored procedure parameters if you are using stored procedures, which are covered in Chapter 11). This avoids any confusion when writing SQL statements or code that manipulates data.

Example 5-1 shows the insertform.cfm template, which creates an HTML form for inputting a new employee record in the EmployeeDirectory table of the ProgrammingCF database.

Example 5-1. HTML input form for inserting a record into a database

<html> <head> <title>Data Input Form</title> </head> <body> <h2>Add a New User</h2> <form action="insert.cfm" method="post"> ...

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.