cfinsert

Description

Inserts records in data sources from data in a ColdFusion form or form Scope.

Category

Database manipulation tags

Syntax

<cfinsert 
  dataSource = "ds_name"
  tableName = "tbl_name"
  tableOwner = "owner"
  tableQualifier = "tbl_qualifier"
  username = "username"
  password = "password"
  formFields = "formfield1, formfield2, ..."> 

See also

cfprocparam, cfprocresult, cfquery, cfqueryparam, cfstoredproc, cftransaction, cfupdate

History

New in ColdFusion MX: the connectString, dbName, dbServer, dbtype, provider and providerDSN attributes are deprecated. Do not use them. They do not work, and might cause an error, in releases later than ColdFusion 5.

Attributes

Attribute Req/Opt Default Description
dataSource
Required
 
Data source; contains table.
tableName
Required
 
Table in which to insert form fields.
ORACLE drivers: must be uppercase.
Sybase driver:case-sensitive. Must be the same case used when table was created
tableOwner
Optional
 
For data sources that support table ownership (such as SQL Server, Oracle, and Sybase SQL Anywhere), use this field to specify the owner of the table.
tableQualifier
Optional
 
For data sources that support table qualifiers, use this field to specify qualifier for table. The purpose of table qualifiers varies among drivers. For SQL Server and Oracle, qualifier refers to name of database that contains table. For Intersolv dBASE driver, qualifier refers to directory where DBF files are located.
username
Optional
 
Overrides username specified in ODBC setup.
password
Optional
 
Overrides password specified in ODBC setup.
formFields
Optional
(all on form, except keys)
Comma-delimited list of form fields to insert. If not specified, all fields in the form are included.
If a form field is not matched by a column name in the database, ColdFusion throws an error.
The database table key field must be present in the form. It may be hidden.

Example

<!--- This shows how to use cfinsert instead of cfquery to put 
data in a datasource. --->
<!--- if form.POSTED exists, we insert new record, so begin cfinsert tag --->
<cfif IsDefined ("form.posted")>
  <cfinsert dataSource = "cfsnippets"
    tableName = "Comments"
    formFields = "Email,FromUser,Subject,MessText,Posted">
  <h3><I>Your record was added to the database.</i></h3>
</cfif>

<cfif IsDefined ("form.posted")> 
  <cfif Server.OS.Name IS "Windows NT"> 
    <cfinsert datasource="cfsnippets" tablename="Comments"
      formfields="EMail,FromUser,Subject,MessText,Posted"> 
  <cfelse> 
    <cfinsert datasource="cfsnippets" tablename="Comments"
      formfields="CommentID,EMail,FromUser,Subject,MessText,Posted"> 
  </cfif> 
  <h3><i>Your record was added to the database.</i></h3> </cfif> 

<!--- use a query to show the existing state of the database --->
<cfquery name = "GetComments" dataSource = "cfsnippets">
  SELECT 
    CommentID, EMail, FromUser, Subject, CommtType, MessText, Posted, Processed
  FROM 
    Comments
</cfquery>

<html>
<head></head>
<h3>cfinsert Example</h3>
<p>First, show a list of the comments in the cfsnippets datasource.
<!--- show all the comments in the db --->
<table>
  <tr>
    <td>From User</td><td>Subject</td><td>Comment Type</td>
    <td>Message</td><td>Date Posted</td>
  </tr>
  <cfoutput query = "GetComments">
    <tr>
      <td valign = top><a href = "mailto:#Email#">#FromUser#</A></td>
      <td valign = top>#Subject#</td>
      <td valign = top>#CommtType#</td>
      <td valign = top><font size = "-2">#Left(MessText, 125)#</font></td>
      <td valign = top>#Posted#</td>
    </tr>
  </cfoutput>
</table>
<p>Next, we'll offer the opportunity to enter a comment:
<!--- make a form for input --->
<form action = "cfinsert.cfm" method = "post">
  <pre>
  Email:   <input type = "Text" name = "email">
  From:   <input type = "Text" name = "fromUser">
  Subject:<input type = "Text" name = "subject">
  Message:<textarea name = "MessText" COLS = "40" ROWS = "6"></textarea>
  Date Posted:   <cfoutput>#DateFormat(Now())#</cfoutput>
  <!--- dynamically determine today's date --->
  <input type = "hidden" 
    name = "posted" value = "<cfoutput>#Now()#</cfoutput>">
  </pre>
  <input type = "Submit" 
    name = "" value = "insert my comment">
</form>

Comments