Avatar of JohnMac328
JohnMac328Flag for United States of America asked on

CF - Form insert one record at a time

I am trying to get a form to insert 1 record at a time.  The form will list several records and one will be selected with a vote button.  I have tried a cfloop index but it is not working.  The error shows that I am trying to insert all the records at one time - I am only testing with two records.  Any help is appreciated.

<cfif  isDefined("FORM.submitMVP")>
 <cfloop index="aRow" from="1" to="#val(form.MVPID)#">
  <cfquery name="enterdetails" datasource="#datasource#">
    INSERT INTO leadervotes (leaderid, voteid, votedate, mvp_ast, votelevel)
    VALUES
    (
        <cfqueryparam value="#form.SessionID#" cfsqltype="cf_sql_varchar"> ,
        <cfqueryparam value="#form.MVPID#" cfsqltype="cf_sql_varchar"> ,
        <cfqueryparam value="#form.mvpdate#" cfsqltype="CF_SQL_varchar"> ,
        <cfqueryparam value="#form.mvp_ast#" cfsqltype="cf_sql_int"> ,
        <cfqueryparam value="#form.votelevelMVP#" cfsqltype="cf_sql_int">
    )
  </cfquery>
  </cfloop>



<cfform action="vote.cfm" method="post" format="html">
<tr>
      <cfinput type="hidden" name="SessionID" value="#Session.ID#">
	  <cfinput type="hidden" name="mvpdate" value="#DateFormat(DateAdd('m', -1, MyDateTime),'YYYYMMDD')#">
      <cfinput type="hidden" name="mvp_ast" value="1">
  <td align="center"><strong><cfoutput>#DateFormat(DateAdd('m', -1, MyDateTime),'mmmm')#</cfoutput></strong></td>
 <cfoutput query="getNominated">
<cfinput type="hidden" name="MVPID" value="#MVPID#">
<tr>
<td>&nbsp;#VoterFirstName#<br />&nbsp;#VoterLastName#</td>
<td width="60%"><strong>#MVPFirstName#</strong>&nbsp;<strong>#MVPLastName#&nbsp;#MVPID#</strong><br />
#mvpreason#</td>
<td>1
  <input type="radio" name="votelevelMVP" value="1" />
  &nbsp;2
  <input type="radio" name="votelevelMVP" value="2" />
  &nbsp;3
  <input type="radio" name="votelevelMVP" value="3" />&nbsp;<input type="submit" name="submitMVP" value="Vote"></td>
</tr>
<!--SEPARATOR ROW USED DIFFERENT STYLE-->
<tr>
  <td colspan="3"></td>
</tr>
</cfoutput>
  </cfform>

Open in new window



Here is the error screen - the numbers 485 and 510 are the MVPID numbers of the records

Error Executing Database Query.
 
 [Macromedia][SQLServer JDBC Driver][SQLServer]Conversion failed when converting the varchar value '485,510' to data type int.  
 
 The error occurred in C:\Inetpub\wwwroot\Voting\vote.cfm: line 92
 
90 :         <cfqueryparam value="#form.mvpdate#" cfsqltype="CF_SQL_varchar"> ,
91 :         <cfqueryparam value="#form.mvp_ast#" cfsqltype="cf_sql_int"> ,
92 :         <cfqueryparam value="#form.votelevelMVP#" cfsqltype="cf_sql_int">
93 :     )
94 :   </cfquery>
ColdFusion LanguageMicrosoft SQL Server

Avatar of undefined
Last Comment
JohnMac328

8/22/2022 - Mon
_agx_

If you want each button to submit one row only, you need to generate separate <form>'s. So move the <form> tag inside the <cfoutput>.

ie

 <cfoutput query="getNominated">

     <!--- BEGIN RECORD FORM --->
       <!--- generate one form per record --->
      <cfform action="vote.cfm" method="post" format="html">
           <cfinput type="hidden" name="SessionID" value="#Session.ID#">
         <cfinput type="hidden" name="mvpdate" value="#DateFormat(DateAdd('m', -1, MyDateTime),'YYYYMMDD')#">
           <cfinput type="hidden" name="mvp_ast" value="1">
           .... etc ....

  </cfform>
  <!--- END RECORD FORM --->

</cfoutput>
ASKER
JohnMac328

Hi agx

When I do it does not see the closing tag anymore

The end tag </cfoutput> encountered on line 152 at column 3 requires a matching start tag.
_agx_

Sounds like maybe you didn't not remove all of the cfoutput tags inside the <form> .  Make sure you've only got one set of <cfoutput...></cfoutput> tags - and that they're outside the <form>

<cfoutput query="getNominated">

      <cfform action="vote.cfm" method="post" format="html">
          ... no cfoutput tags here
       </cfform>

</cfoutput>
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
JohnMac328

Ok it ran but it filled the whole table with the one record I clicked on
ASKER CERTIFIED SOLUTION
_agx_

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
JohnMac328

Thanks again as always agx!