I am trying to enter data from an array into a database and keep getting an error.
My array is generated from a form and then converted to a Session structure because I will be adding it in the database later. Here is the code for this:
<cfset Sleeps = 6> <!---Temporary value set - This will come from a database--->
<cfform name="formGuests" id="formGuests" method="post">
<h3>Please enter the names, ages and relationship to person booking the property. If less than guests, leave the additional spaces blank.</h3>
<table class="guests">
<tr><td width="255">Guest(s)</td><
td width="55">Age</td><td width="155">Relationship</
td></tr>
</table>
<cfloop from="1" to="#Sleeps#" index="i">
<label for="Guest#i#">
<cfif i EQ 1>
<cfinput type="text" name="Guest#i#" size="35">
<cfinput type="text" name="Age#i#" size="3">
<cfinput type="hidden" name="Relationship#i#" value="Remitter">
<cfelse>
<cfinput type="text" name="Guest#i#" size="35">
<cfinput type="text" name="Age#i#" size="3">
<cfinput type="text" name="Relationship#i#">
</cfif>
</label><br />
</cfloop>
<br />
<cfinput type="hidden" name="maxOccupancy" value="6">
<cfinput type="submit" name="SubmitForBooking" id="SubmitForBooking" value="Continue" />
</cfform>
<cfif IsDefined("SubmitForBookin
g")>
<cfset arrayGuest = []>
<!--- for each line in the form, extract the values and add it to your array --->
<cfloop from="1" to="#form.maxOccupancy#" index="i">
<!--- create a new structure to store the new line of information --->
<cfset guestInfo = {} >
<!--- store the values of each form field --->
<cfset guestInfo.guest = FORM["Guest#i#"]>
<cfset guestInfo.age = FORM["Age#i#"]>
<cfset guestInfo.relationship = FORM["Relationship#i#"]>
<!--- add the new guest to your array --->
<cfset arrayAppend(arrayGuest, guestInfo)>
</cfloop>
<!---Create the Session struction for the Guests array--->
<cflock timeout="20" scope="session" type="exclusive">
<cfset SESSION.Guests = StructNew()>
</cflock>
<!---- Dump the data to see the results --->
<cfset Session.Guests.arrayGuest = arrayGuest>
<cfdump var="#Session.Guests#">
</cfif>
Here is the code I am using to enter into the database:
<cfset OrderID = 1> <!---Temporary value set - This will come from a database--->
<cfloop from="1" to="6" index="i">
<cftransaction>
<cfquery datasource="#DataSource#" name="insertGuests">
INSERT INTO guests
(OrderID, GuestName, GuestAge, Relationship)
VALUES (<cfqueryparam value="#Trim(OrderID)#" cfsqltype="cf_sql_integer"
>,
<cfqueryparam value="#Trim(Session.Guest
s.arrayGue
st.Guest[i
])#" cfsqltype="cf_sql_varchar"
>,
<cfqueryparam value="#Trim(Session.Guest
s.arrayGue
st.Age[i])
#" cfsqltype="cf_sql_integer"
>,
<cfqueryparam value="#Trim(Session.Guest
s.arrayGue
st.Relatio
nship[i])#
" cfsqltype="cf_sql_varchar"
>)
</cfquery>
</cftransaction>
</cfloop>
The error I am getting is:
Element GUESTS.ARRAYGUEST.GUEST is undefined in a Java object of type class [Ljava.lang.String; referenced as ''