14 Replies Latest reply on Sep 13, 2006 9:18 AM by Newsgroup_User

    Looping insert using CFC

    Level 7
      I am stuck on this. I have a form that loops the number of textboxes based
      on a user choice. I want to enter these into a database using a CFC, but it
      keeps telling me that the textbox title 'name' does not exist and I am
      confused on how to procede. I attempted to change the name/id to just 'name'
      but that didn't go over well as expected.
      Here is the form, the invoke, and then the CFC.
      Please help, its the last step of a project that I want to get finished.
      Everything works just fine, except for the insert. Thanks!

      Form:
      <form id="addguests" name="addguests" method="post" action="">
      <table width="100%" border="0">
      <tr>
      <td colspan="2">
      <cfloop index="i" from="1" to="#Form.guests#">
      <table width="100%" border="0">
      <cfoutput>
      <tr>
      <td><div align="right"><strong>Guest #i#
      Name:</strong></div></td>
      <td><input name="name#i#" " type="text" id="name#i#"
      " value="" size="25" /></td>
      </tr>
      <tr>
      <td><div align="right"><strong>Guest #i#
      Description:</strong></div></td>
      <td><input name="description#i#" type="text"
      id="description#i#" " value="" size="65" /><input name="show_id"
      type="hidden" id="show_id" value="#URL.Show#" />
      <input name="guest" id="guest" value="#nog#" type="hidden" /> </td>
      </tr>
      </cfoutput></table></cfloop></td>
      </tr>
      <tr>
      <td width="25%"> </td>
      <td width="75%"><input name="Submit_Add" type="submit"
      class="small_buttn" id="Submit_Add" value="Add Guests" /></td>
      </tr>
      </table>
      </form>

      Invoke:
      <cfif IsDefined ('Submit_Add')>
      <cfloop index="i" from="1" to="#Form.guest#">
      <cfinvoke component="#Shows#" method="AddGuests">
      <cfinvokeargument name="show" value="#URL.show#">
      </cfinvoke>
      </cfloop>
      <cflocation url="index.cfm?show=added" addtoken="no">
      </cfif>

      CFC:
      <cffunction name="AddGuests" access="public" returntype="void">
      <cfargument name="show" type="numeric" required="yes">
      <cfquery name="AddGuests" datasource="mydb">
      INSERT INTO RadioShowGuests (name, descript, show)
      Values ('#Form.name#', '#Form.description#', #arguments.show#)
      </cfquery>
      </cffunction>


        • 1. Re: Looping insert using CFC
          Level 7
          I get this error:

          Element NAME is undefined in FORM.


          The error occurred in E:\www\cfcs\radioshow\show.cfc: line 112

          110 : <cfquery name="AddGuests" datasource="sql_portal">
          111 : INSERT INTO RadioShowGuests (name, descript, show)
          112 : Values ('#Form.name#', '#Form.description#', #arguments.show#)
          113 : </cfquery>
          114 : </cffunction>



          • 2. Re: Looping insert using CFC
            Level 7
            It's not particularly good design to write CFC's that expect to access
            outside data. This is what you have done by having your cfc function
            trying to access data from the form scope outside of itself.

            A better method would be to pass in those values as arguments to the cfc
            function.

            <cffunction ...>
            <cfargument name="show" ...>
            <cfargument name="name" ...>
            <cfargument name="description" ...>
            ...
            VALUES (arguments.name, arguments.description, arguments.show)
            ...
            </cffunction>

            Then when you invoke this function you would pass all the data from to
            the function.

            <cfinvoke component="#shows# method="addGuests">
            <cfinvokeargument name="show" value="#URL.show#">
            <cfinvokeargument name="name" value="#form.name#">
            <cfinvokeargument name="description" value="#form.description#">
            </cfinvoke>

            I do not know if this will have any relevance to your original question
            or now?

            It looks like you are not properly identifying your fields. In your
            form code you are creating fields with names like name1, name2, name3
            and description1, description2, description3. But you are trying to
            access those with fields as just form.name and form.description. You
            need to include the numbers when access those fields, IE. name1 and
            description3.
            • 3. Re: Looping insert using CFC
              Level 7
              I am attempting to do a looping insert using a form and a CFC that inserts
              it into the database.

              It keeps telling me that #form.name# is undefined...which it is...swear to
              god.

              How can I have the form sumbit the multiple field fields and have the cfc
              pick them up and insert them multiple times?

              I thought perhaps if I changed the invoke to:

              <cfloop index="i" from="1" to="#Form.guest#">
              <cfinvoke component="#Shows#" method="AddGuests">
              <cfinvokeargument name="show" value="#URL.show#">
              <cfinvokeargument name="name" value="#form.name#i##">
              <cfinvokeargument name="description" value="#form.description#i##">
              </cfinvoke>
              </cfloop>

              It might work....nope.

              I just want the user to choose how many fields they want to insert, then
              loop the fields based on that number, then insert the information into the
              database using a cfc. I am lost.....

              Thanks for any info.


              • 4. Re: Looping insert using CFC
                Level 7
                Choosing one set of field works perfectly. Multiples crash..


                • 5. Re: Looping insert using CFC
                  Level 7
                  Here is the full thing incase anyone is bored:

                  <cfobject name="Shows" component="cfcs.radioshow.show">
                  <!---Step 2: Add photos and captions--->
                  <cfif IsDefined ('Submit_Add')>
                  <cfloop index="i" from="1" to="#Form.guest#">
                  <cfinvoke component="#Shows#" method="AddGuests">
                  <cfinvokeargument name="show" value="#URL.show#">
                  <cfinvokeargument name="name" value="#form.name#i##">
                  <cfinvokeargument name="description" value="#form.description#i##">
                  </cfinvoke>
                  </cfloop>
                  <cflocation url="index.cfm?show=added" addtoken="no">

                  </cfif>

                  <table width="98%" border="0">
                  <tr>
                  <td bgcolor="silver"><table width="99%" border="0" align="center">
                  <tr>
                  <td width="40%"><b>Radio Show Administrator</b></td>
                  <td width="60%"><div align="center"><a href="index.cfm">Home</a> |
                  <a href="createshow.cfm">Create A New Radio Show</a> | <a
                  href="archivedate.cfm">Change Archive Date</a></div></td>
                  </tr>
                  </table></td>
                  </tr>
                  <tr>
                  <td height="233" valign="top" bgcolor="#eeeeee"><table width="99%"
                  border="0">
                  <tr>
                  <td> </td>
                  </tr>

                  <tr>
                  <td><table width="99%" border="0">
                  <tr>
                  <td colspan="2"><b>Step 3: Add Guests to the program </b></td>
                  </tr>
                  <tr>
                  <td colspan="2">
                  <cfif Not IsDefined ('guests_number')>
                  <table width="100%" border="0">
                  <tr>
                  <td><cfoutput><form id="guests" name="guests"
                  method="post" action="guestslist.cfm?show=#URL.show#"></cfoutput>
                  Choose the number of guests you would like to add:
                  <select name="guests" id="guests">
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                  <option value="5">5</option>
                  <option value="6">6</option>
                  <option value="7">7</option>
                  <option value="8">8</option>
                  <option value="9">9</option>
                  <option value="10">10</option>
                  </select>
                  <input name="guests_number" type="submit" id="guests_number"
                  value="Submit" />
                  </form> </td>
                  </tr>
                  </table></cfif> </td>
                  </tr>
                  <tr>
                  <td colspan="2">
                  <cfif IsDefined ('guests_number')>
                  <cfset nog = #Form.guests#>
                  <form id="addguests" name="addguests" method="post" action="">
                  <table width="100%" border="0">
                  <tr>
                  <td colspan="2">
                  <cfloop index="i" from="1" to="#Form.guests#">
                  <table width="100%" border="0"><cfoutput>
                  <tr>
                  <td><div align="right"><strong>Guest #i#
                  Name:</strong></div></td>
                  <td><input name="name" type="text" id="name"
                  value="" size="25" /></td>
                  </tr>
                  <tr>
                  <td><div align="right"><strong>Guest #i#
                  Description:</strong></div></td>
                  <td><input name="description" type="text"
                  id="description" value="" size="65" /><input name="show_id" type="hidden"
                  id="show_id" value="#URL.Show#" />
                  <input name="guest" id="guest" value="#nog#" type="hidden" /> </td>
                  </tr>
                  </cfoutput></table></cfloop></td>
                  </tr>
                  <tr>
                  <td width="25%"> </td>
                  <td width="75%"><input name="Submit_Add" type="submit"
                  class="small_buttn" id="Submit_Add" value="Add Guests" /></td>
                  </tr>
                  </table>
                  </form></cfif>
                  </td>
                  </tr>
                  <tr>
                  <td width="24%"> </td>
                  <td width="76%"> </td>
                  </tr>
                  </table></td>
                  </tr>
                  </table></td>
                  </tr>
                  </table>
                  <p>
                  <cfelse>

                  The CFC Looks like this now:

                  <cffunction name="AddGuests" access="public" returntype="void">
                  <cfargument name="show" type="numeric" required="yes">
                  <cfargument name="name" type="string" required="yes">
                  <cfargument name="description" type="string" required="yes">
                  <cfquery name="AddGuests" datasource="mydb">
                  INSERT INTO RadioShowGuests (name, descript, show_id)
                  Values ('#arguments.name#', '#arguments.description#', #arguments.show#)
                  </cfquery>
                  </cffunction>


                  • 6. Re: Looping insert using CFC
                    Level 7
                    > It keeps telling me that #form.name# is undefined...which it is...swear to
                    > god.

                    First lesson in debugging: get that sort of thinking out of your head. The
                    computer is always right. If it tells you you are mistaken: it's right,
                    you're wrong. If it tells you there is a problem, it's because there *IS*
                    a problem.

                    So stop thinking "but I *know* I'm right". This will not help you. Focus
                    on working out WHY you're wrong.

                    And the first thing to do in this case is, immediately before you use the
                    form variable that the computer is saying does not exist... Do a <cfdump
                    variable="#form#">. You will see that the computer is correct, and the
                    evidence in front of you should go some way to identifying where you've
                    gone wrong, and accordingly how to fix it.

                    I believe it's probably more useful for you to work through this yourself
                    and understand where you're going wrong than posting your code and saying
                    "hey, pls fix this for me". Of course you might disagree, and I'm sure Ian
                    is about to pop up with the direct answer to your question ;-)

                    --
                    Adam

                    >
                    > How can I have the form sumbit the multiple field fields and have the cfc
                    > pick them up and insert them multiple times?
                    >
                    > I thought perhaps if I changed the invoke to:
                    >
                    > <cfloop index="i" from="1" to="#Form.guest#">
                    > <cfinvoke component="#Shows#" method="AddGuests">
                    > <cfinvokeargument name="show" value="#URL.show#">
                    > <cfinvokeargument name="name" value="#form.name#i##">
                    > <cfinvokeargument name="description" value="#form.description#i##">
                    > </cfinvoke>
                    > </cfloop>
                    >
                    > It might work....nope.
                    >
                    > I just want the user to choose how many fields they want to insert, then
                    > loop the fields based on that number, then insert the information into
                    the
                    > database using a cfc. I am lost.....
                    >
                    > Thanks for any info.
                    • 7. Re: Looping insert using CFC
                      Level 7
                      Actually Adam, as valid and true your advice was, it unfortunately is
                      not the issue with Wally's code. When he dumps his form, which is
                      always a good way to see what is being passed around, it is going to
                      show him what he expects to see.

                      The problem is his syntax in trying to append a number to a string to
                      build a form field name.

                      <cfinvokeargument name="name" value="#form.name#i##">
                      <cfinvokeargument name="description" value="#form.description#i##">

                      You can not nest pound signs like that. "#form.name#i##" is trying to
                      build a string that made up of the value of form.name, which does not
                      exist, "i" and a pound sign "#".

                      Take a look at the documentation for array notation to access structures
                      such as the form structure. Something like this is what you want in
                      your value parameters - form["name" & i]
                      • 8. Re: Looping insert using CFC
                        Level 7
                        > Actually Adam, as valid and true your advice was, it unfortunately is
                        > not the issue with Wally's code.

                        It's not the ONLY thing wrong with his code, no.

                        However, if he's seeing that error, then - indeed - something is afoot, and
                        form.name doesn't exist. If the last lot of code he posted is indeed that
                        which he is running, it's odd, as they variable ought to exist. The
                        indexing thing you mention will cause him other problems, but that's not
                        the source of his immediate error. The code's not getting far enough for
                        your problem to present itself.

                        The initial problem is - as the error states - form.name is not defined.
                        This is "interesting" as according to the last iteration of Wally's code,
                        there is indeed a form variable called name:

                        <input name="name" type="text" id="name"
                        value="" size="25" />

                        I suspect the problem is that this is NOT the form code he is running, he's
                        still running the original code he quoted:

                        <input name="name#i#" " type="text" id="name#i#"
                        " value="" size="25" />

                        Either way, as you confirm, #form.name#i## is NOT right, whichever way one
                        looks at it. And if the input is the second one I quoted just above, then
                        your suggestion is spot on.

                        However I was kinda hoping we'd leave Wally to work this out for himself,
                        hence my suggestion of trying the <cfdump> as a general troubleshooting
                        approach to helping solve this and subsequent problems. I believe it's
                        better to teach a person to fish rather than handing them a fish burger.

                        --
                        Adam
                        • 9. Re: Looping insert using CFC
                          Level 7
                          I appreciate the debate between the two of you. I actually was running the
                          <input name="name" type="text" id="name" > value="" size="25" /> code, but
                          out of desperation, I changed it to the latter.

                          I know the problem is that I am trying to loop the insert through the CFC. I
                          have done this before using the insert statement inside the code. That one
                          works fine. This is why I am confused.

                          I would love to work out the problem for myself, but I don't understand the
                          problem. I think I can narrow it down to the aforementioned problem, but I
                          have no idea on where to start to figure it out.

                          As much as I appreciate both a solution to this problem (since the project
                          needs to get done and I learn by see the answer and reverse engineering the
                          problem), if either of you two can point me to the correct area of the
                          problem so I can start to research.

                          I am a wee confused on the #Form.name# problem. As far as I can see, it
                          isn't a variable value problem, so dumping it didn't solve anything. It was
                          saying that the variable itself didn't exist. Since I am looking at <input
                          name="name" type="text" id="name" > value="" size="25" /> and that it what
                          is being passed.

                          By only choose one loop, it worked fine. By increasing the looped fields, it
                          crashes. By deduction, I can figure it has something to do with the fact
                          that the field name is increasing with each loop.

                          I was hoping that looping the call to the CFC and incrementing that also
                          would solve that issue, however that is not the case.

                          Any ideas or further advice would be greatly appreciated.



                          • 10. Re: Looping insert using CFC
                            Level 7
                            On Tue, 12 Sep 2006 19:58:58 -0400, Wally Kolcz wrote:

                            > I am a wee confused on the #Form.name# problem. As far as I can see, it
                            > isn't a variable value problem, so dumping it didn't solve anything. It was
                            > saying that the variable itself didn't exist.

                            Right.

                            And when you dumped the form scope, WAS IT THERE?

                            I'm presuming not, as you're saying you're getting an error to that effect.

                            Doing the dump effects two things:
                            1) Stops you from thinking "it DOES exist!".
                            2) Demonstrates what IS being passed in the form scope, which should put
                            you in the right direction for working out WHY it's not there.

                            After that is the next bit about you referencing dynamic variables
                            correctly, and Ian has already pointed you in the right direction here:

                            <blockquote>
                            You can not nest pound signs like that. "#form.name#i##" is trying to
                            build a string that made up of the value of form.name, which does not
                            exist, "i" and a pound sign "#".

                            Take a look at the documentation for array notation to access structures
                            such as the form structure. Something like this is what you want in
                            your value parameters - form["name" & i]
                            </blockquote>

                            --
                            Adam
                            • 11. Re: Looping insert using CFC
                              Level 7
                              Well, I am a the first to admit that I am an array retard. So I have to
                              build and array and then push it to the CFC for the insert?



                              • 12. Re: Looping insert using CFC
                                Level 7
                                > Well, I am a the first to admit that I am an array retard. So I have to
                                > build and array and then push it to the CFC for the insert?

                                "Take a look at the documentation for array notation to access structures
                                such as the form structure. "

                                So, you know... you should take a look at the documentation for array
                                notation to access structures such as the form structure.

                                --
                                Adam
                                • 14. Same idea, different way
                                  Level 7
                                  What is the best type to use to create a 'temporary' database to add,
                                  update, or remove from prior to being dumped to the database as one insert?

                                  How do you associate the data to the database columns?

                                  Told you I am retarded, but willing to learn.