Skip to main content
Known Participant
December 12, 2012
Question

update multiple fields with same/diffrent name

  • December 12, 2012
  • 2 replies
  • 939 views

I have the form is displayed  order no, message and sent_date. Mesage and Date sent are editable.  I can change value from both colums in diffrent values then hit submit for update.  My code below is loop thur message and date sent field then update them based on the pk ID.  Unfortunately, it didn't work as the expected.  Can you please help?

Oder NOMessageDate Sent
5463first message12-10-12
5463second message10-13-12

<cfset myIds = ArrayNew(1)>

<cfform name="update" method="post">

<cfloop query="qMesg">

   <cfscript>

        ArrayAppend(myIds, "#id#");

        </cfscript>

       

     <cfinput type="text" value="#mesg#" name="mesg">

     <cfinput type="text" value="#dateformat(sent_date, 'mm-dd-yy')#" name="sent_date" validate="date" />

     <cfinput type="submit" name="submit" value="Update">

</cfloop>

  <cfset myIdsList = #ArrayToList(myIds, ",")#>

   <cfinput type="hidden" name="ids" value="#myIdsList#">

</cfform>

<!---update--->

<cfif isDefined("form.submit")>

<cfloop index="idIdx" list="#newsids#" delimiters=",">

<cfloop list="#form.mesg#" index="x">

<cfloop list="#form.sent_date#" index="y">

      update [tblMessg]

     set mesg = <cfqueryparam value="#x#" cfsqltype="cf_sql_varchar">,

           sent_date = <cfqueryparam value="#y#" cfsqltype="cf_sql_date">

   where id = <cfqueryparam value="#idIdx#" cfsqltype="cf_sql_integer">

  </cfloop>

</cfloop>

</cfloop>

</cfif>

This topic has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
December 13, 2012

I am with Dan on this (identifying the field names with IDs), but prefer cfloop to cfoutput within cfform. Furthermore, I would put the submit button outside the loop.

<cfloop query="qMesg">

    <cfscript>

    ArrayAppend(myIds, "#id#");

    </cfscript>

    <cfinput type="text" value="#mesg#" name="mesg#id#">

    <cfinput type="text" value="#dateformat(sent_date, 'mm-dd-yy')#" name="sent_date#id#" validate="date" />

</cfloop>

<cfinput type="submit" name="submit" value="Update">

With proper handling, one loop should be sufficient on the action page.

<cfloop index="idIdx" list="#form.ids#" delimiters=",">

    <cfset current_message = form["mesg" & idIdx]>

    <cfset current_date_sent = form["sent_date" & idIdx]>

    <cfquery>

    UPDATE tblMessg

    SET mesg = <cfqueryparam value="#current_message#" cfsqltype="cf_sql_varchar">,

    sent_date = <cfqueryparam value="#current_date_sent#" cfsqltype="cf_sql_date">

    WHERE id = <cfqueryparam value="#idIdx#" cfsqltype="cf_sql_integer">

    </cfquery>

</cfloop>

Inspiring
December 12, 2012

My approach is to do something like this:

on the form page

<cfoutput query= something>

<input name = staticpart#id field from query#>

</cfoutput>

on the processing page

<cfloop list = form.fieldnames index = thisfield>

<cfif left(thisfield, 10) is "staticpart">

<cfscript>

ThisID = replace(thisfield, "staticpart", "");

ThisStaticPartValue = form[thisfield];

ThisOtherValue = form["OtherStaticPart" & ThisID];

then do something with those values