cfupdate

Updates records in a data source from data in a ColdFusion form or form Scope.

Database manipulation tags

<cfupdate 
dataSource = "ds_name"
tableName = "table_name"
tableOwner = "name"
tableQualifier = "qualifier"
username = "username"
password = "password"
formFields = "field_names">

cfinsert, cfprocparam, cfprocresult, cfquery, cfqueryparam, cfstoredproc, cftransaction

ColdFusion MX: Deprecated the connectString, dbName, dbServer, dbtype, provider and providerDSN attributes. They do not work, and might cause an error, in releases later than ColdFusion 5.

Attribute Req/Opt Default Description

dataSource

Required

 

Name of the data source that contains the table.

tableName

Required

 

Name of table to update.

  • For ORACLE drivers, must be uppercase.
  • For Sybase driver: case-sensitive; must be in same case as used when the table was created

tableOwner

Optional

 

For data sources that support table ownership (for example, SQL Server, Oracle, Sybase SQL Anywhere), the table owner.

tableQualifier

Optional

 

For data sources that support table qualifiers. The purpose of table qualifiers is as follows:

  • SQL Server and Oracle: name of database that contains table
  • Intersolv dBASE driver: directory of DBF files

username

Optional

 

Overrides username value specified in ODBC setup.

password

Optional

 

Overrides password value specified in ODBC setup.

formFields

Optional

(all on form, except keys)

Comma-delimited list of form fields to update.

If a form field is not matched by a column name in the database, ColdFusion throws an error.

The formFields lies must include the database table primary key field, which must be present in the form. It can be hidden.

<!--- This example shows the use of CFUPDATE to change
records in a datasource. --->
<!--- if course_ID has been passed to this form, then
perform the update on that record in the datasource --->

<cfif IsDefined("form.Course_ID")>
   <!--- check that course_id is numeric --->
      <cfif Not IsNumeric(form.Course_ID)>
         <cfabort>
      </cfif>
   <!--- Now, do the update --->
   <cfupdate datasource="cfsnippets"
      tablename="Courses" 
      formfields="Course_ID,Number,Descript">
</cfif>

<!--- Perform a query to reflect any updated information if Course_ID is passed
through a url, we are selecting a record to update ... select only that 
record with the WHERE clause. --->
<cfquery name="GetCourseInfo" DATASOURCE="cfsnippets">
   SELECT Course_Number, Course_ID, Descript
   FROM Courses
      <cfif IsDefined("url.Course_ID")>
         WHERE Course_ID = #Trim(url.Course_ID)#
      </cfif>
   ORDER by Course_Number
</cfquery>
<html>
<head>
   <title>CFUPDATE Example</title>
   <cfset css_path = "../../css">
   <cfinclude template="../../resource/include/mm_browsersniff.cfm">
</head>
<body>

<H3>CFUPDATE Example</H3>
<!--- If we are updating a record, don't show the entire list. --->
<cfif IsDefined("url.Course_ID")>
   <form method="post" action="index.cfm">
   <H3>You can alter the contents of this record, and then click "Update" 
      to use CFUPDATE and alter the database</H3>
   <P>Course Number <INPUT TYPE="Text" name="Number" value="<cfoutput>#Trim(GetCourseInfo.Course_Number)#</cfoutput>">
   <P>Course Description<BR>
   <textarea name="Descript" cols="40" rows="5">
      <cfoutput>#Trim(GetCourseInfo.Descript)#</cfoutput>
   </textarea><br>
   <input type="Hidden" NAME="Course_ID"
      value="<cfoutput>#Trim(GetCourseInfo.Course_ID)#</cfoutput>">
   <p><input type="Submit" value="Click to Update">
   </form>

<cfelse>
   <!--- Show the entire record set in CFTABLE form --->
   <cftable query="GetCourseInfo" htmltable colheaders>
      <cfcol text="<a href='index.cfm?Course_ID=#Trim(Course_ID)#'>Edit Me</a>" 
         width=10 header="Edit<br>this Entry">
      <cfcol text="#Trim(Course_Number)#" WIDTH="4" HEADER="Course Number">
      <cfcol text="#Trim(Descript)#" WIDTH=100 HEADER="Course Description">
   </cftable>
</cfif>
</body>
</html>

View comments on LiveDocs