1 Replies - 693 Views - Last Post: 03 October 2016 - 01:16 PM Rate Topic: -----

#1 malibu65k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-May 16

Loop multidimentional array in cfc cffunction to insert into SQL

Posted 03 October 2016 - 10:34 AM

This works…
$.ajax({url: '../cfc/saveData.cfc', data: {method: 'addAccountInfo', acctNo: aID, firstName: firstName, lastName: astName, address1: address1, address2: address2, phone1: phone1, phone2: phone2, rowID:i}, success: function(data){return true;}});

my cffunction in cfc...
<cffunction name = "addAccountInfo" access="remote" returntype="any" returnformat="plain">
        <cfargument name = "acctNo" required="yes" type="numeric">
        <cfargument name = "firstName" required="yes" type="string">
        <cfargument name = "lastName" required="yes" type="string">
        <cfargument name = "address1" required="yes" type="string">
        <cfargument name = "address2" required="yes" type="string">
        <cfargument name = "phone1" required="yes" type="string">
        <cfargument name = "phone2" required="yes" type="string">
        <cfargument name = "rowID" required="yes" type="numeric">
             <cfquery name="addInfo">
                    INSERT INTO acctInfo
                                         (acctNo,
                                          firstName,
                                          lastName,
                                          address1,
                                          address2,
                                          phone1,
                                          phone2,
                                           rowID)
                              VALUES (<cfqueryparam cfsqltype="cf_sql_numeric" value="#aID#">,
                                      <cfqueryparam cfsqltype="cf_sql_varchar" value="#firstName#">,
                                      <cfqueryparam cfsqltype="cf_sql_varchar" value="#lastName#">,
                                      <cfqueryparam cfsqltype="cf_sql_varchar" value="#address1#">,
                                      <cfqueryparam cfsqltype="cf_sql_varchar" value="#address2#">,
                                      <cfqueryparam cfsqltype="cf_sql_varchar" value="#phone1#">,
                                      <cfqueryparam cfsqltype="cf_sql_varchar" value="#phone2#">,
                                      <cfqueryparam cfsqltype="cf_sql_numeric" value="#i#">)
             </cfquery>
</cffunction>

But now, I've placed the data in an array called fldData and I've passed the array to the cffunction in the cfc using...
$.ajax({url: '../cfc/savaData.cfc?method=addAccountInfo', data: fldData, success: function(data){return true;}});	

Now the insert doesn't work. Nothing inserts. I used console.log and the data is there in an array. I've tried many suggestions but nothing works so far, so I figured maybe I needed to ask my own question.
Any assistance would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Loop multidimentional array in cfc cffunction to insert into SQL

#2 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 2024
  • View blog
  • Posts: 3,600
  • Joined: 13-January 08

Re: Loop multidimentional array in cfc cffunction to insert into SQL

Posted 03 October 2016 - 01:16 PM

The issue is that you have an array called "fldData" but you're not doing anything in the component method to handle that incoming array so that you can insert the records in that array into your database. You lack a CFARGUMENT entry for fldData and you're not trying to loop over an array to either insert each record or create variables to insert via query.

For instance, how would your method even know that you've passed it an array to work with? You're passing in a single data object but your method has 8 required parameters to pass in. You need to wipe the existing CFARGUMENTs and specify a new one to handle the incoming array. Once you have the array over to the method, you loop over the array to figure out what data got passed in for each array iteration...and then you go from there.

Check out the links in my response and they should get you pointed in the right direction.

Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1