StructInsert

Inserts the specified key-value pair into the specified structure. Returns Yes if the insert was successful and No if an error occurs.

See also StructClear, StructCount, StructFind, StructIsEmpty, StructKeyArray, StructDelete, StructKeyExists, and StructUpdate.

Syntax

StructInsert(structure, key, value [, allowoverwrite ])
structure

Structure to contain the new key-value pair.

key

Key that contains the inserted value.

value

Value to be added.

allowoverwrite

Optionally indicates whether to allow overwriting an existing key. The default is FALSE.

Usage

This function throws an exception if structure does not exist or if key exists and allowoverwrite is set to FALSE.

Example

<!---------------------------------------------------------------------- 
This example shows how to use the StructNew, StructInsert, and StructFind 
functions. 
----------------------------------------------------------------------->
<HTML>
<HEAD>
<title>Add New Employees</title>
</HEAD>

<BODY>
<H3>Add New Employees</H3>
<!--- Establish parms for first time through  --->
<CFIF NOT IsDefined("FORM.firstname")>
    <CFSET FORM.firstname="">
</CFIF>    
<CFIF NOT IsDefined("FORM.lastname")>
    <CFSET FORM.lastname="">
</CFIF>        
<CFIF NOT IsDefined("FORM.email")>
    <CFSET FORM.email="">
</CFIF>        
<CFIF NOT IsDefined("FORM.phone")>
    <CFSET FORM.phone="">
</CFIF>        
<CFIF NOT IsDefined("FORM.department")>
    <CFSET FORM.department="">
</CFIF>        

<CFIF FORM.firstname EQ "">
 <P>Please fill out the form.
<CFELSE>
    <CFSET employee=StructNew()>
    <CFSET retCode=StructInsert(employee, "firstname", FORM.firstname)>
    <CFSET retCode=StructInsert(employee, "lastname", FORM.lastname)>
    <CFSET retCode=StructInsert(employee, "email", FORM.email)>
    <CFSET retCode=StructInsert(employee, "phone", FORM.phone)>
    <CFSET retCode=StructInsert(employee, "department", FORM.department)>

    <CFOUTPUT>
    <P>First name is #StructFind(employee, "firstname")#</P>
    <P>Last name is #StructFind(employee, "lastname")#</P>
    <P>EMail is #StructFind(employee, "email")#</P>
    <P>Phone is #StructFind(employee, "phone")#</P>
    <P>Department is #StructFind(employee, "department")#</P>
    </CFOUTPUT>
<!----------------------------------------------------------------------
    Write code to add the employee in the database.
----------------------------------------------------------------------->
</CFIF>

<Hr>
<FORM action="structinsert.cfm" method="post">
<P>First Name:&nbsp;
<INPUT name="firstname" type="text" hspace="30" maxlength="30">
<P>Last Name:&nbsp;
<INPUT name="lastname" type="text" hspace="30" maxlength="30">
<P>EMail:&nbsp;
<INPUT name="email" type="text" hspace="30" maxlength="30">
<P>Phone:&nbsp;
<INPUT name="phone" type="text" hspace="20" maxlength="20">
<P>Department:&nbsp;
<INPUT name="department" type="text" hspace="30" maxlength="30">

<P>
<INPUT type="submit" value="OK">
</FORM>

</BODY>
</HTML>