Skip to main content
September 8, 2010
Question

Loop over form values & insert into db

  • September 8, 2010
  • 3 replies
  • 3449 views

Form

prod_id     prod_name       prod_price    prod_status
001         product 001     1.00          1            
002         product 002     2.00          1      
003         product 003     3.00          0      
004         product 004     4.00          0      

Form Dump
prod_name      product 001,product 002,product 003,product 004
FIELDNAMES     prod_id, prod_name, prod_price, prod_status
prod_price     1.00,2.00,3.00,4.00
prod_status    1,1,0,0
prod_id        001,002,003


I want to update a few fields, prod_price and prod_status. I submit these values to my update page but I'm not sure how to loop over these values and update by the prod_id. In my update page I'm using cfdump and I see the structure of the form, the fieldnames as well as the prod_id, prod_name, etc and their values.

My question is how do I loop over these form values so I'll be able to update by each prod_id? I've played around with looping over the form collection values but no luck.

    This topic has been closed for replies.

    3 replies

    September 9, 2010

    Thanks guys, I'll take a look at your suggestions. I did dig deeper through this site and found this thread - http://forums.adobe.com/message/416352#416352, could be helpful to anyone that finds ths question later on.

    Inspiring
    September 8, 2010

    When I do stuff like this, I append the "id" value to the end of each form field.  That enables me to ensure the other fields match up.

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

    <cfloop list="#form.fieldnames# index="ThisField">

    <cfif left(ThisField, 9) is "prod_name">

    <cfscript>

    ThisID = removechars(thisfield, 9);

    ThisName = form["prod_name" & ThisID];

    same for other fields

    Then do something with those variables.

    Inspiring
    September 8, 2010

    If each form field (prod_name, prod_price, etc.) contains a comma delimited list of values you can use CF's list functions to parse the data.

    http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec1a60c-7ffc.html#WSc3ff6d0ea77859461172e0811cbec22c24-6a42