Chapter 5. Maintaining Database Records
Now that we’ve covered the basics of retrieving records from a database, let’s focus our attention on techniques you can use to add new records, update existing records, and delete unwanted records from a database. These techniques come into play when your web application needs to go beyond simply displaying information from a database. For example, with our employee directory application, we can add the ability to insert new employees into the directory, update employee information, and delete employee records.
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 is generally 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.
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. 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 ...
Get Programming ColdFusion 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.