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 fields 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 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.
The following table describes the code and its function:
Code |
Description |
---|---|
<cfif not isdefined("Form.Contract")> <cfset Form.contract = "N"> <cfelse> <cfset form.contract = "Y"> </cfif> |
Sets the value of Form.Contract to No if it is not defined, or to Yes if it is defined. If the Contractor check box is unchecked, no value is passed to the action page; however, the database field must have some value. |
<cfupdate datasource="cfdocexamples" tablename="EMPLOYEE"> |
Updates the record in the database that matches the primary key on the form (Emp_ID). Updates all fields in the record with names that match the names of form controls. |
<cfoutput> You have updated the information for #Form.FirstName# #Form.LastName# in the employee database. </cfoutput> |
Informs the user that the change was made successfully. |