You usually use 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 message for the end user that reports on the update completion.
An update form is similar to an insert form, but there are two key differences:
A primary key is a field or combination of fields in a database table that uniquely identifies each record in the table. For example, in a table of employee names and addresses, only the Emp_ID would be 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.
<cfquery name="GetRecordtoUpdate"
datasource="CompanyInfo">
SELECT *
FROM Employee
WHERE Emp_ID = #URL.Emp_ID#
</cfquery>
<html>
<head>
<title>Update Form</title>
</head>
<body>
<cfoutput query="GetRecordtoUpdate">
<form action="updateaction.cfm" method="Post">
<input type="Hidden" name="Emp_ID"
value="#Emp_ID#"><br>
First Name:
<input type="text" name="FirstName" value="#FirstName#"><br>
Last Name:
<input type="text" name="LastName" value="#LastName#"><br>
Department Number:
<input type="text" name="Dept_ID" value="#Dept_ID#"><br>
Start Date:
<input type="text" name="StartDate" value="#StartDate#"><br>
Salary:
<input type="text" name="Salary" value="#Salary#"><br>
Contractor:
<cfif #Contract# IS "Yes">
<input type="checkbox" name="Contract" checked>Yes<br>
<cfelse>
<input type="checkbox" name="Contract">Yes<br>
</cfif>
<br>
<input type="Submit" value="Update Information">
</form>
</cfoutput>
</body>
</html>
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 cfupdate, you must include the field or fields that make up the primary key in your form submittal. The cfupdate tag automatically detects the primary key fields in the table that you are updating and looks for them in the submitted form fields. ColdFusion uses the primary key fields to select the record to update. (Therefore, you cannot update the primary key value itself.) It then uses the remaining form fields that are submitted to update the corresponding fields in the record. Your form only needs to have fields for the database fields that you want to change.
<cfif not isdefined("Form.Contract")>
<cfset form.contract = "No">
<cfelse>
<cfset form.contract = "Yes">
</cfif>
<cfupdate datasource="CompanyInfo"
tablename="Employee">
<html>
<head>
<title>Update Employee</title>
</head>
<body>
<h1>Employee Updated</h1>
<cfoutput>
You have updated the information for #Form.FirstName#
#Form.LastName# in the Employees database.
</cfoutput>
</body>
</html>
updateaction.cfm.updateform.cfm in a browser by specifying the page URL and an Employee ID, for example, http://localhost/myapps/updateform.cfm?Emp_ID=3. Enter new values in any of the fields, and click the Submit button.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.
cfupdate tag with the highlighted cfquery code.:<cfif not isdefined("Form.Contract")>
<cfset form.contract = "No">
<cfelse>
<cfset form.contract = "Yes">
</cfif>
<cfquery name="UpdateEmployee" datasource="CompanyInfo">
UPDATE Employee
SET FirstName = '#Form.Firstname#',
LastName = '#Form.LastName#',
Dept_ID = '#Form.Dept_ID#',
StartDate = '#Form.StartDate#',
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 Employees database.
</cfoutput>
The following table describes the highlighted code and its function:
Return to Firstserv.com | ColdFusion Hosting | Macromedia LiveDocs