You usually use the following two application pages to update data in a database:
You can create an update form with cfform
tags or HTML form tags. The update form calls an update action page, which can contain either a cfupdate
tag or a cfquery
tag with a SQL UPDATE statement. The update action page should also contain a confirmation message for the end user.
The following are the key differences between an update form and an insert form:
A primary key is a field(s) in a database table that uniquely identifies each record. For example, in a table of employee names and addresses, only the Emp_ID is unique to each record.
The easiest way to designate the primary key in an update form is to include a hidden input field with the value of the primary key for the record you want to update. The hidden field indicates to ColdFusion which record to update.
<html> <head> <title>Update Form</title> </head> <body> <cfquery name="GetRecordtoUpdate" datasource="CompanyInfo"> SELECT * FROM Employee WHERE Emp_ID = #URL.Emp_ID# </cfquery> <cfoutput query="GetRecordtoUpdate"> <table> <form action="update_action.cfm" method="Post"> <input type="Hidden" name="Emp_ID" value="#Emp_ID#"><br> <tr> <td>First Name:</td> <td><input type="text" name="FirstName" value="#FirstName#"></td> </tr> <tr> <td>Last Name:</td> <td><input type="text" name="LastName" value="#LastName#"></td> </tr> <tr> <td>Department Number:</td> <td><input type="text" name="Dept_ID" value="#Dept_ID#"></td> </tr> <tr> <td>Start Date:</td> <td><input type="text" name="StartDate" value="#StartDate#"></td> </tr> <tr> <td>Salary:</td> <td><input type="text" name="Salary" value="#Salary#"></td> </tr> <tr> <td>Contractor:</td> <td><cfif #Contract# IS "Yes"> <input type="checkbox" name="Contract" checked>Yes <cfelse> <input type="checkbox" name="Contract">Yes </cfif></td> </tr> <tr> <td> </td> <td><input type="Submit" value="Update Information"></td> </tr> </form> </table> </cfoutput> </body> </html>
http://localhost/myapps/update_form.cfm?Emp_ID=3
Note: Although you can view an employee's information, you must code an action page before you can update the database. For more information, see "Creating an action page to update data".
The following table describes the code and its function:
You can create an action page to update data with either the cfupdate
tag or cfquery
with the UPDATE statement.
The cfupdate
tag is the easiest way to handle simple updates from a front-end form. The cfupdate
tag has an almost identical syntax to the cfinsert
tag.
To use the cfupdate
tag, you must include the primary key field(s) in your form submittal. The cfupdate
tag automatically detects the primary key field(s) in the table that you are updating and looks for them in the submitted form fields. ColdFusion uses the primary key field(s) to select the record to update (therefore, you cannot update the primary key value itself). It then uses the remaining form fields that you submit to update the corresponding fields in the record. Your form only needs to have fields for the database fields that you want to change.
<html> <head> <title>Update Employee</title> </head> <body> <cfif not isdefined("Form.Contract")> <cfset form.contract = "No"> <cfelse> <cfset form.contract = "Yes"> </cfif> <cfupdate datasource="CompanyInfo" tablename="Employee"> <h1>Employee Updated</h1> <cfoutput> You have updated the information for #Form.FirstName# #Form.LastName# in the employee database. </cfoutput> </body> </html>
http://localhost/myapps/update_form.cfm?Emp_ID=3
The current information for that record appears:
ColdFusion updates the record in the Employee table with your new values and displays a confirmation message.
The following table describes the code and its function:
For more complicated updates, you can use a SQL UPDATE statement in a cfquery
tag instead of a cfupdate
tag. The SQL UPDATE statement is more flexible for complicated updates.
The following procedure assumes that you have created the update_action.cfm page as described in "Creating an update action page with cfupdate".
cfupdate
tag with the following highlighted cfquery
code:<html> <head> <title>Update Employee</title> </head> <body> <cfif not isdefined("Form.Contract")> <cfset form.contract = "No"> <cfelse> <cfset form.contract = "Yes"> </cfif> <!--- cfquery requires date formatting when retrieving from Access. Use the left function when setting StartDate to trim the ".0" from the date when it first appears from the Access database ---> <cfquery name="UpdateEmployee" datasource="CompanyInfo"> UPDATE Employee SET FirstName = '#Form.Firstname#', LastName = '#Form.LastName#', Dept_ID = #Form.Dept_ID#, StartDate = '#left(Form.StartDate,19)#', Salary = #Form.Salary# WHERE Emp_ID = #Form.Emp_ID# </cfquery> <h1>Employee Updated</h1> <cfoutput> You have updated the information for #Form.FirstName# #Form.LastName# in the employee database. </cfoutput> </body> </html>
http://localhost/myapps/update_form.cfm?Emp_ID=3.
ColdFusion updates the record in the Employee table with your new values and displays a confirmation message.
When the cfquery
tag retrieves date information from a Microsoft Access database, it displays the date with tenths of seconds, as follows:
This example uses the left
function to trim the two final characters. The CompanyInfo data source connects to company.mdb.
The following table describes the highlighted code and its function: