使用MySql在Coldfusion表单中插入动态数量的行

时间:2015-10-08 14:30:16

标签: jquery mysql forms loops coldfusion

我已经接管了一个在Coldfusion表单中使用JQuery来动态添加项目行的项目,用于引用"引用"文献。我需要循环通过表单并将每一行插入到单独字段中的MySql数据库中。我的问题是我需要通过动态更改名称和值多次添加一次和其他值。 这是形式:(我省略了一些只需要插入一次的字段)

    <tr>
    <input id="quoteDate" name="quoteDate"  value="#rc.private.currDate#">
    <input id="quoteID" name="quoteID" value="#rc.private.quoteID#">
    </tr>
    <tr id="dummyRow" class="hidden-print" style="display:none">
    <td>                
    <input id="itemQty_" name="itemQty_" value="" class="qtyField" type="number" min="0" pattern="[0-9]*" maxlength="6" size="3">
    </td>                           
    <td>                            
<input id="itemPriceEach_" name="itemPriceEach_" class="itemPriceField" type="text" maxlength="8" value="">
    <input id="itemDesc_" name="itemDesc_" class="hidden" value="">
    </td>

最后3个输入是使用以下jQuery动态添加的输入:

   nr = $newRow;
    $newRow.removeAttr("id");
    $newRow.find("td:nth-child(1)").empty().append( item.itemCode );
    $newRow.find("td:nth-child(2)").empty().append( item.description );
    $newRow.find("input[id^=itemQty_]").val(1).attr("id", "itemQty_" + item.itemCode) .attr("name", "itemQty_" + item.itemCode);
    $newRow.find("input[id^=itemPriceEach_]").val( accounting.formatMoney(item.price) ).attr("id", "itemPriceEach_" + item.itemCode).attr("name", "itemPriceEach_" + item.itemCode);
    $newRow.find("input[id^=itemDesc_]").val( item.description ).attr("id","itemDesc_" + item.description).attr("name","itemDesc_" + item.description);

如何仅通过所需的输入循环以插入具有不同名称和值的输入。我已经构建的内容已经将所有值都插入到表中的一个字段中。

<cfquery name="addNewQuote" datasource="inv">
            INSERT INTO inv.quotesaves
                (quoteID, cNumber, quoteDate, itemcode, itemQty_, itemPriceEach_, itemDesc_,fetqty, fetPriceEach)
            VALUES(
                <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.quoteID#">,
                <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.cNumber#">,
                <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.quoteDate#">,
                <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.itemCode#">,
                <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.itemQty_#">,
                <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.itemPriceEach_#">,
                <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.itemDesc_#"> )               
</cfquery>              

这是我的表格转储。

-CNUMBER    ZZ1753

-FIELDNAMES QUOTEDATE,QUOTEID,CNUMBER,ITEMCODE,ITEMDESC_51769,ITEMQTY_51769,ITEMPRICEEACH_51769,ITEMQTY_015,ITEMPRICEEACH_015,ITEMDESC_LABOR,ITEMQTY_011,ITEMPRICEEACH_011,ITEMDESC_OIL,ITEMQTY_,ITEMPRICEEACH_,ITEMDESC_,ADDITEMCODE

-ITEMCODE   51769

-ITEMDESC_  [empty string]

-ITEMDESC_51769 [empty string]

-ITEMDESC_LABOR labor

-ITEMDESC_OIL   oil

-ITEMPRICEEACH_ [empty string]

-ITEMPRICEEACH_011  $3.00

-ITEMPRICEEACH_015  $15.00

-ITEMPRICEEACH_51769    $157.70

-ITEMQTY_   [empty string]

-ITEMQTY_011    1

-ITEMQTY_015    3

-ITEMQTY_51769  2

-QUOTEDATE  October 08, 2015

-QUOTEID    82103808

action  retailquote.quotesave
origPage    RetailQuote.quoteSave   

我需要的是以&#34; itemQty _&#34;开头的每个项目。和&#34; itemPriceEach _&#34;要在单独的字段中添加到表中。因此,一行将包含&#34; quoteID&#34;,&#34; itemQty _&#34;的值和#34; itemPriceEach _&#34;

经过多次试验和磨难,我终于想出了一个使用cfloop和ListGetAt做到这一点的好方法。似乎以这种方式完美地工作。

<cftransaction>
    <cfloop from="1" to="#ListLen(form.itemQty_)#" index="i">
        <cfquery name="quoteAddOns"  datasource="inv">
            INSERT INTO inv.quoteAddOns
                (quoteID, itemDesc_,itemQty_, itemPriceEach_, fetqty, fetPriceEach)
            VALUES(
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.quoteID#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#ListGetAt(arguments.itemDesc_,i)#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#ListGetAt(arguments.itemQty_,i)#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#ListGetAt(arguments.itemPriceEach_,i)#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#ListGetAt(arguments.fetQty#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.fetPriceEach#">)
    </cfquery>
</cfloop>

1 个答案:

答案 0 :(得分:0)

(评论太长)

使用&#34;相同命名字段&#34;的缺点方法是,如果任何值包含列表分隔符(即逗号),代码将中断,这并非罕见。列表的常见替代方法之一是generating unique field names by appending a numeric ID value as a suffix

例如,假设您有三个itemCode&#34; 51769,51775,51782&#34;,只需将每个代码作为后缀添加到所有相关的字段(数量) ,desc,price,etcetera),即

  • itemPrice_ 51769 ,itemDesc_ 51769 ,itemQty_ 51769 ,...
  • itemPrice_ 51775 ,itemDesc_ 51775 ,itemQty_ 51775 ,...
  • itemPrice_ 51782 ,itemDesc_ 51782 ,itemQty_ 51782 ,...

要提取提交的值,只需循环遍历form.itemCode列表,并使用关联数组表示法动态访问相关字段:

<cfloop list="#form.itemCode#" index="currItemCode">

   <!--- extract the fields for current itemCode --->
   <cfset itemQty   = FORM["itemQty_"& currItemCode]>
   <cfset itemPrice = FORM["itemPrice_"& currItemCode]>
   <cfset itemDesc  = FORM["itemDesc_"& currItemCode]>
   ...

   <!--- for debugging only ---> 
   <cfoutput>
   DEBUG: Current itemCode = #currentID# 
          itemQty   = #itemQty#
          itemPrice = #itemPrice#
          itemDesc  = #itemDesc#
     ... <br>
   </cfoutput>


   ... now use the values in your query ....
</cfloop>

如果你必须坚持所有&#34;同名字段&#34;,更好的选择可能是新的CF10 + Application setting this.sameformfieldsasarray。具有相同名称的字段将作为数组而不是列表传递,这样可以避免分隔符问题。只需遍历生成的数组。