If you know the number range you're using, and there are no gaps,
something like this should do the trick
<cfloop from="1" to="#upperbound#" index="i">
<cfquery datasource="#dsn#">
INSERT INTO ANSWERS
(
QUESTION_ID,
ANSWER
)
VALUES
<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#i#">,
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#form["Q" &
i]#">
)
</cfquery>
</cfloop>
The key is using "array notation" rather than "dot notation":
#form["fieldname"]
-----Original Message-----
From: Joel Watson [mailto:[EMAIL PROTECTED]
Sent: Tuesday, March 06, 2007 7:37 AM
To: CF-Talk
Subject: Insert Looped Form Entry into Database
I have a cfform that is dynamically populated with test questions and
answer choices from a database. The answer choices are also dynamically
named based upon the id of the question, e.g., input
name="Q#QuestionID#". I am currently able, upon submit, to pass the
values of user-selected answers, producing this:
struct
FIELDNAMES SUBMIT,Q1,Q2,Q3,Q4,Q5,Q6,Q7
Q1 Mexico City
Q2 Seattle
Q3 London
Q4 Istanbul
Q5 True
Q6 HEre is an essay question
Q7 Here is an essay question
SUBMIT Submit Test
My question is how do I capture the fieldnames and values (i.e., Q1 -
Mexico City) so that I can place them in a database when the fieldnames
are being dynamically created? I initially tried an insert
qualification like '#FORM.Q#QuestionID##', but obviously this does not
work as CF complains about "Q" being undefined. Any ideas would be
greatly appreciated!
Joel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Macromedia ColdFusion MX7
Upgrade to MX7 & experience time-saving features, more productivity.
http://www.adobe.com/products/coldfusion
Archive:
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:271689
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4