User Guide Cancel

cfinsert

 

Note:

This tag is unsupported in CFFiddle.

Description

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

Category

Syntax

<cfinsert
dataSource = "data source name"
tableName = "table name"
fetchClientInfo = "true|false"
formFields = "formfield1, formfield2, ..."
password = "password"
tableOwner = "owner"
tableQualifier = "table qualifier"
username = "user name">
<cfinsert dataSource = "data source name" tableName = "table name" fetchClientInfo = "true|false" formFields = "formfield1, formfield2, ..." password = "password" tableOwner = "owner" tableQualifier = "table qualifier" username = "user name">
<cfinsert 
dataSource = "data source name" 
tableName = "table name" 
fetchClientInfo = "true|false" 
formFields = "formfield1, formfield2, ..." 
password = "password" 
tableOwner = "owner" 
tableQualifier = "table qualifier" 
username = "user name">
Note:

You can specify this tag's attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag's attribute names as structure keys.

See also

History

  • ColdFusion (2025 release): Removed connectString, dbName, dbServer, dbType, provider, and providerDSN attributes.
  • ColdFusion 10: Added the attribute fetchClientInfo and clientInfo.
  • 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.

Attributes

Attribute

Req/Opt

Default

Description

dataSource

Optional

 

Data source; contains table.

clientInfo

Optional

 

Structure containing properties of the client to be set on the database connection.

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

fetchClientInfo

Optional

false

If set true, returns a struct with the key-value pair passed by the last query.

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.

password

Optional

 

Overrides password specified in ODBC setup.

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.

Example

<!--- This example 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 = "cfdocexamples"
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="cfdocexamples" tablename="COMMENTS"
formfields="EMail,FromUser,Subject,MessText,Posted">
<cfelse>
<cfinsert datasource="cfdocexamples" 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 = "cfdocexamples">
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 cfdocexamples 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, well 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>
<!--- This example 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 = "cfdocexamples" 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="cfdocexamples" tablename="COMMENTS" formfields="EMail,FromUser,Subject,MessText,Posted"> <cfelse> <cfinsert datasource="cfdocexamples" 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 = "cfdocexamples"> 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 cfdocexamples 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, well 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>
<!--- This example 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 = "cfdocexamples" 
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="cfdocexamples" tablename="COMMENTS" 
formfields="EMail,FromUser,Subject,MessText,Posted"> 
<cfelse> 
<cfinsert datasource="cfdocexamples" 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 = "cfdocexamples"> 
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 cfdocexamples 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, well 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>
Note:

The cfinsert tag internally uses parameterized queries.

Get help faster and easier

New user?