Skip navigation
Currently Being Moderated

Questions - Multiple Inserts

Oct 15, 2010 7:34 AM

I am making a question form - which has multiple questions - which would then

insert multiple answers (yes/no/na) and notes into a table.

 

I'm at a loss on how to do it - and having a brain fart...

 

Here is the loop to create the questions... Radio button for answer...

 

<cfloop query=Q StartRow=1 EndROW="#Column1NumRows#">
<tr>
<td valign=top>
<cfoutput>
#qid#.
</cfoutput>
</td>
<td width=300 valign=top>
<cfoutput>
#question#
<cfif len(notes1)><br>#notes1#: <cfinput type="text" name="notes1" value="" size=10></cfif>   
<cfif len(notes2)><br>#notes2#: <cfinput type="text" name="notes2" value="" size=10></cfif>
</cfoutput>
</td>
<td align=center> Yes<br><cfinput type="Radio" name="answer#qid#" value="Yes"> </td>
<td align=center> No<br><cfinput type="Radio" name="answer#qid#" value="No"> </td>
<td align=center> N/A<br><cfinput type="Radio" name="answer#qid#" value="N/A"> </td>
</tr>
<tr height=2 bgcolor=grey><td colspan=5></td></tr>
</cfloop>

But I am having trouble inserting into the answer table..

 

I'm assuming I need to loop thru things...

I also need to attribute this to a single user...

 

Any help is appreciated...

 
Replies
  • Currently Being Moderated
    Oct 15, 2010 7:43 AM   in reply to Merle_Hanson

    Put underscores into your form field names on the radio buttons. Once you've submitted your form, you'll have a whole load of form fields named something like this:

     

    answer_1 = Y

    answer_2 = N

    answer_3 = Y

    answer_4 = N/A

    etc

     

    So, you need to loop through all your form fields and do an insert.

     

    <cfloop collection="#form#" item="key">

      <cfif find("answer_", key)>

        <cfset ThisQuestion = listLast(key, '_')  />

        <cfset ThisAnswer = form[key] />

        <cfquery...>

          INSERT INTO answers (....)

          VALUES ( <cfqueryparam ThisQuestion />, <cfqueryparam ThisAnswer />) etc

        </cfquery>

      </cfif>

    </cfloop>

     

    Not the most efficient, but that's probably not the major concern here

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 15, 2010 11:58 AM   in reply to Merle_Hanson

    The general idea will work.  However, the most important part, as usual, is making sure that your database schema is optimal for what you are trying to accomplish. 

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points