Skip to main content
Known Participant
November 18, 2008
Question

Help with update

  • November 18, 2008
  • 6 replies
  • 1434 views
I query a table by a part number and retrieve all the line items for that part number. I then display in a table using <cfoutput query>. At the end of each row, I want to add another text field for comments, <cfinput type="text" name="comments" size="30">, somthing like that.

To associate each comments text field with the others, I would use #currentrow# or some type of row id ?
How do I update the table so that each record/row is updated accordingly with the corresponding comments text ?
    This topic has been closed for replies.

    6 replies

    Inspiring
    November 28, 2008
    > I get this error message about variable1 is not in the scope,
    > or something like that.

    Can you post a snippet of the form and action page code?
    Inspiring
    November 19, 2008
    New York Guy wrote:
    > The comments field is just a simple html : <cfinput type="text"
    > name="comments" size="30">. I have this displayed at the end
    > of each row, with the other columns dipsplayed only.

    As Dan pointed out, the form fields should have unique names. Whether you use the recordID or a counter number is up to you. But the names should be unique, so you can extract the individual values.

    > So far you have been presented with two differing opinions as to
    > what that variable should be. I stand by my recommendation that
    > it be the id of the record you intend to update.
    > ...
    > <cfif left(ThisField, 7) is "comment">

    I prefer the counter number approach, as it avoids potential field name conflicts. For example, let us say you add another unrelated field also starting with "comment". The code might error or do something unexpected.
    Inspiring
    November 19, 2008
    That will give you a comma delimeted list of all the comments. You want a those fields named "comments#somevariable#". So far you have been presented with two differing opinions as to what that variable should be. I stand by my recommendation that it be the id of the record you intend to update.
    Inspiring
    November 19, 2008
    > I tried both methods, since they were similar, and got the following error :
    > Element comments1 is undefined in a Java object of type class

    The examples are not copy and paste code, but see my correction above about the field name:

    ie .. #form['comment s'& ...]# should be #form['comment ID'& ...]#.

    Inspiring
    November 19, 2008
    I'm guessing you are thinking a one to many relationship between parts and comments. I thought it was a field in the table.

    Only the New York Guy knows for sure.
    Known Participant
    November 19, 2008
    I tried both methods, since they were similar, and got the following error :

    Element comments1 is undefined in a Java object of type class coldfusion.filter.FormScope referenced as The specific sequence of files included or processed...

    Also, comments is in the table but there are no entries. I am attempting to update it here.
    Inspiring
    November 19, 2008
    quote:

    Originally posted by: New York Guy
    I tried both methods, since they were similar, and got the following error :

    Element comments1 is undefined in a Java object of type class coldfusion.filter.FormScope referenced as The specific sequence of files included or processed...

    Also, comments is in the table but there are no entries. I am attempting to update it here.

    How did you generate the form fields in which the user can enter comments?
    Inspiring
    November 19, 2008
    You would use the id value somewhere in the name of the input. For example, comment1, comment2, etc.

    When processing the form, I would do something like this:

    <cfloop list = "#form.formfields#" index = "ThisField">
    <cfif left(ThisField, 7) is "comment">
    <cfif len(form[ThisField] ) gt 0>
    <cfset ThisID = removechars(ThisField, 7)>
    <cfquery>
    update thetable
    set comments = <cfqueryparam value="#form[ThisField]#">
    where id = <cfqueryparam value = #ThisID#>

    closing tags
    Inspiring
    November 19, 2008
    Another variation is to name the fields using #currentRow#

    <cfoutput query="yourQuery">
    <input type="text" name="commentID#currentID#" ...>
    <input type="hidden" name="recordID#currentID#" ...>
    </cfoutput>

    <cfoutput>
    <input type="hidden" name="totalRows" value="#yourQuery.recordCount#">
    </cfoutput>

    The do a similar loop to perform the updates

    <cfloop from="1" to="#form.totalRows#" index="row">
    <cfquery>
    UPDATE YourTable
    SET Comments = <cfqueryparam value="#form['comments'& row]#" ...>
    WHERE recordID = Comments = <cfqueryparam value="#form['recordID'& row]#" ...>
    </cfquery>
    </cfloop>
    Inspiring
    November 19, 2008
    > <input type="text" name="commentID#currentID#" ...>
    > <input type="hidden" name="recordID#currentID#" ...>

    That should be #currentRow# not #currentID#