I have a form that I submit and returns me about 500 rows. Before inserting into table I check the strings are not null. This means it loops all the strings.

Is there a better way that it doesn't have to check all the values?

Only wheel_winner will be an empty string, and emp_id will always have a number.


    <td><input type="text"  name="wheel_winner#CurrentRow#" id="sum" value=""></td>
    <td> #emp_namefirst# 
    <input type="hidden" name="emp_id#CurrentRow#" value="#emp_id#"></td>

        <input type="Hidden" name="ResCount" value="#GetEmployees.RecordCount#">
<!-- after submitting values -->
<cfloop from="1" to="#ResCount#" index="j">
    <cfset  emp_id  = #Trim(form["emp_id" & j])#>
    <cfset  WHEEL_WINNER    = #Trim(form["WHEEL_WINNER" & j])#>

<cfif WHEEL_WINNER neq "" and emp_id neq "" >
        <cfquery datasource="test" >
            INSERT INTO stamps (emp_id, wheel_winner )
            VALUES (<cfqueryPARAM value = "#emp_id#" CFSQLType = 'CF_SQL_INTEGER'>, 
            <cfqueryPARAM value = "#wheel_winner#" CFSQLType = 'CF_SQL_INTEGER'>)

  • 1
    \$\begingroup\$ When the form is submitted, the empty values are sent over too. I suppose you could add a javascript function that removes the empty fields from the DOM, but I would not go down that road \$\endgroup\$ – James A Mohler Jan 5 '16 at 19:43
  • 2
    \$\begingroup\$ Unless you encountering some sort of issue, there is nothing wrong with looping. (Though the extra pound signs are unnecessary here). There is really no way to avoid validating each pair of values. Even if you could validate in JS first, it is not a good idea to rely on client side validation. Always back it up with server side validation. \$\endgroup\$ – Leigh Jan 13 '16 at 14:20
  • \$\begingroup\$ What database are you using? \$\endgroup\$ – Adrian Lynch Apr 14 '16 at 10:16
  • \$\begingroup\$ @AdrianLynch sql 2012 \$\endgroup\$ – anatp_123 Apr 15 '16 at 0:35
  • \$\begingroup\$ This code looks fine. It looks strange were you stop cfoutput then start again. But otherwise it is OK \$\endgroup\$ – James A Mohler May 11 '16 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.