Monday, March 22, 2010

Update/Edit a database through...

Hey all.聽 I have been trying to get this to work for two days and am stuck.聽 I want to be able to edit a database through a cfajaxproxy function.聽 Here is what I have:

%26lt;cfajaxproxy cfc=''#application.cfcRoot#schedulerFunctions'' jsclassname=''schedulerProxy''%26gt;
聽聽聽聽聽聽聽 %26lt;script%26gt;
聽聽聽聽聽聽聽聽聽聽聽 var schedulerProxy = new schedulerProxy();

聽聽聽聽聽聽聽聽聽聽聽 schedulerProxy.setCallbackHandler( successHandler );
聽聽聽聽聽聽聽聽聽聽聽 schedulerProxy.setErrorHandler( failHandler );

聽聽聽聽聽聽聽聽聽聽聽 function updateGame( gameID, divID )
聽聽聽聽聽聽聽聽聽聽聽 {

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 // grab these two values from two selection elements on the page
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 var homeTeam = document.getElementById( ''select1'' );
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 var visitorTeam = document.getElementById( ''select2'' );
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 gameProxy.updateGameMini( gameID, homeTeam.value, visitorTeam.value );
聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽 function successHandler( result )
聽聽聽聽聽聽聽聽聽聽聽 {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 alert( result );
聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽 function failHandler( statusCode, statusMsg )
聽聽聽聽聽聽聽聽聽聽聽 {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 alert( ''failure'' );
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 alert(statusCode+': '+statusMsg);
聽聽聽聽聽聽聽聽聽聽聽 }聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 %26lt;/script%26gt;

And I have the aforementioned cfc with this.聽 In it, I want to update the games field with the two values.聽 So the meat of the function is:

聽聽聽 %26lt;cffunction name=''updateGameMini'' access=''remote'' returntype=''query'' output=''false''%26gt;
聽聽聽聽聽聽聽 %26lt;cfargument name=''ID'' type=''string'' requried=''yes'' hint=''Game ID''%26gt;
聽聽聽聽聽聽聽 %26lt;cfargument name=''homeTeam'' type=''string'' required=''yes'' hint=''Home team unique ID''%26gt;
聽聽聽聽聽聽聽 %26lt;cfargument name=''visitorTeam'' type=''string'' required=''yes'' hint=''Visitor team unique ID''%26gt;

聽聽聽聽聽聽聽聽聽 %26lt;cfquery name=''myUpdateGame'' datasource=''#application.datasource#'' username=''#application.dbUser#'' password=''#application.dbPass#''%26gt;
聽聽聽聽聽聽聽聽聽聽聽 UPDATE games
聽聽聽聽聽聽聽聽聽聽聽 SET home=%26lt;cfif arguments.homeTeam EQ ''''%26gt;NULL%26lt;Cfelse%26gt;%26lt;cfqueryparam cfsqltype=''cf_sql_varchar'' value=''#arguments.homeTeam#''%26gt;%26lt;/cfif%26gt;,
聽聽聽聽聽聽聽聽聽聽聽 visitor=%26lt;cfif arguments.visitorTeam EQ ''''%26gt;NULL%26lt;Cfelse%26gt;%26lt;cfqueryparam cfsqltype=''cf_sql_varchar'' value=''#arguments.visitorTeam#''%26gt;%26lt;/cfif%26gt;
聽聽聽聽聽聽聽聽聽聽聽 WHERE gameID=%26lt;cfqueryparam cfsqltype=''cf_sql_varchar'' value=''#arguments.ID#''%26gt;
聽聽聽聽聽聽聽 %26lt;/cfquery%26gt;

聽聽聽聽聽聽聽聽聽 %26lt;cfreturn myUpdateGame%26gt;

聽聽聽 %26lt;/cffunction%26gt;

But I keep getting ''500: Internal Server Error''.聽 Why?聽 Is this possible to run an UPDATE database call through a cfajaxproxy cfc function?

Update/Edit a database through...

Have you successfully tested this cfc by calling it without using ajax?聽 If not, do so.聽 If you get the same error, the problem will be with your cfc.

Update/Edit a database through...

One of the tests I ran was to use a QUERY sql call instead of the listed UPDATE one, and it ran fine.聽 So I don't think the problem is with the cfc.聽 Truth be told, I lifted that code directly from another bit of code that has existed in the codebase for a while.聽 But that cfc function was a ''public'' one, not ''remote'', and I didn't want to change code that wasn't mine and risk breaking something else.聽 So I copied it over and just changed the access.

Does this mean there should not be any problem, hypothetically, with updating a database through such a mechanism?


I don't use ajax because I'm still on cf7 but I don't see why what you are attempting would be impossible.聽 Unfortunately the error message you are getting is not very helpful.

You have to find out what is causing the problem.聽聽 I suggest starting by copying the update query to a cfm file, putting hard values in place of the variables,聽 and running it.聽 If that works, move it to a local function and run it again, still with the hard values.聽 If that works, start sending it arguments, one at a time.聽 Eventually you will find the problem.


The correct object is schedulerProxy, not gameProxy.


Found the problem.聽 You were right, there was a mistake in the cfc, but I wasn't able to see it until I remove it from AJAX.聽 Apparently, I copied the original code a little too closely, and was repeating function and query names.聽 Works well, thanks for the help.

No comments:

Post a Comment