我有一些动态创建表单控件的表单代码。我需要将每个动态生成的表单字段插入到单独的表格行中:例如,如果为一个表单派生5个问题,则用户将输入答案,需要将questionid和answer插入到表格Questions:questionID,answers。
如何循环FORM
获取结果?这是我尝试过的:
<cfform name="form" method="post" action="TestQA.cfm">
<table border="0">
<tr>
<td> </td>
<td>question</td>
<td> </td>
</tr>
<cfoutput query="rsQuestions">
<tr>
<td><cfinput type="hidden" name="question_id"
id="question_id"
value="#rsQuestions.question_id#">
</td>
<td>#rsQuestions.question#</td>
<td>
<cfif #rsQuestions.question_type_id# eq 1>
<cfinput type="text" name="answer"
message="#rsQuestions.Message#"
tooltip="#rsQuestions.Tooltip#"
range="#rsQuestions.range#"
required="Yes"
size="#rsQuestions.Size#"
method="Post">
</cfif>
</td>
</tr>
</cfoutput>
<tr>
<td> </td>
<td> </td>
<td><cfinput type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
<br />
</cfform>
将问题插入表格的过程:
<cfloop collection=#form# item="field">
<cfif field neq 'submit'>
<cfquery datasource="dstest">
INSERT INTO dbo.group_member_questionnaire_answers (question_id, answer)
VALUES(#form.Question_ID#, #form.Answer#)
</cfquery>
</cfif>
</cfloop>
<cfoutput>Answers inserted successfully into database</cfoutput>
我对此进行了调整:
<cfloop from="1" to="#val(form.TotalFields)#" index="x">
<cfquery datasource="dsED">
INSERT INTO dbo.group_member_questionnaire_answers (question_id, answer)
VALUES(#form.Question_ID#, #form.Answer#)
</cfquery>
</cfloop>
This is the result: Error Executing Database Query.
[Macromedia][SQLServer JDBC Driver][SQLServer]Invalid column name 'no'.
The error occurred in
7 : <cfquery datasource="dstest">
8 : INSERT INTO dbo.group_member_questionnaire_answers (question_id, answer)
9 : VALUES(#form.Question_ID#, #form.Answer#)
10 : </cfquery>
11 :
________________________________________
SQL INSERT INTO dbo.group_member_questionnaire_answers (question_id, answer) VALUES(62,63, 90,no)
DATASOURCE dsED
VENDORERRORCODE 207
SQLSTATE 42S22
Resources: