2012-02-15 19 views
2

嘗試執行插入操作時,我從Railo中得到一個無益的SQL錯誤。我認爲這與我傳遞給它的param值有關。因爲當我把它們取出來並用一個簡單的字符串替換它時,它就起作用了。ColdFusion/Railo腳本中「新查詢()」的SQL錯誤

這裏是我的代碼(它坐落在CFC內):

local.registerUserQuery = new query(); 
      local.registerUserQuery.setDatasource("popwave"); 
      local.registerUserQuery.setSQL(" 

       INSERT INTO users (

        userUUID, 
        userName, 
        userPassword, 
        userEmail, 
        userToken, 
        userCreated, 
        userBiography, 
        userCommentPoints, 
        userLinkPoints, 
        userImageID, 
        userRemoved, 
        userCategoriesOwner, 
        userCategoriesSubscribed 

       ) 

       VALUES (

        '#createUUID()#' , 
        :userName , 
        :userPassword , 
        :userEmail , 
        '#local.token#' , 
        #createODBCDate(now())# , 
        '' , 
        0, 
        0, 
        0, 
        0, 
        0, 
        0 

       ) 

      "); 

      local.registerUserQuery.setName("registerUser"); 

      local.registerUserQuery.addParam(name="userName", value="#arguments.userName#", cfsqltype="cf_sql_varchar"); 
      local.registerUserQuery.addParam(name="userPassword", value="#arguments.userPassword#", cfsqltype="cf_sql_varchar"); 
      local.registerUserQuery.addParam(name="userEmail", value="#arguments.userEmail#", cfsqltype="cf_sql_varchar"); 

      local.registerUserQuery.execute(); 

這是爲什麼拋出一個錯誤,我不能瞭解我的生活!我需要能夠使用:param's。

這裏的Railo錯誤:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 49 

檢查該行號似乎並沒有指向任何東西相關。這個查詢就像200行!而且,正如前面提到的......當我替換這些參數值時,SQL似乎工作正常。

實際上我在這裏做錯了嗎?把我的頭髮拉出來!

謝謝, 邁克爾。

編輯:

忘了提,Railo版本是:

Railo 3.3.1.000 Error (database) 

回答

4

聽起來像您遇到的問題RAILO-1281應該在3.3.1.005中修復。

"RAILO-1281: When doing a query INSERT with CFScript "new query()" and using a cfqueryparam with addParam(), the end ")" is removed"

另外,您應該使用addParam作爲所有動態值。

+0

好的謝謝。我現在已經恢復使用基於標籤的CFC。我曾嘗試使用cfscript,但我發現它還沒有達到它。好吧!謝謝(你的)信息。 – 2012-02-15 18:51:37

+0

順便說一句,除了升級評論提到一個修復,你可以手動應用到你的'query.cfc'文件很容易。但這取決於你。 – Leigh 2012-02-15 18:55:31

0

試試這個

local.registerUserQuery.setSQL(reReplace(" 

      INSERT INTO users (

       userUUID, 
       userName, 
       userPassword, 
       userEmail, 
       userToken, 
       userCreated, 
       userBiography, 
       userCommentPoints, 
       userLinkPoints, 
       userImageID, 
       userRemoved, 
       userCategoriesOwner, 
       userCategoriesSubscribed 

      ) 

      VALUES (

       '#createUUID()#' , 
       :userName , 
       :userPassword , 
       :userEmail , 
       '#local.token#' , 
       #createODBCDate(now())# , 
       '' , 
       0, 
       0, 
       0, 
       0, 
       0, 
       0 

      ) 

     ", "\t|\n", " ", "all")); 
+0

這似乎不能解決任何問題,但會產生另一個錯誤:\t功能權限的參數2現在必須是正數[0]。我試過手動刪除間距;沒有運氣。 – 2012-02-15 17:42:57

0

不知道這是你的問題,但它的使用是個好主意addParam用於所有變量,而不僅僅是sql注入風險。

它確保它們以正確的格式正確傳遞並且執行一個好習慣。也許我對此有錯,但是不需要在mySql的單引號內部輸入#createODBCDateTime#? addParam會爲你做這個東西。

+0

我不認爲它會導致一個問題,因爲查詢工作時,我只是刪除所有:參數值。該日期進入日期/時間列的類型,所以它不是一個varchar,因此免除了引號的需要,我認爲。 – 2012-02-15 17:39:59

+0

@MichaelGiovanniPumo看到這篇文章:http://stackoverflow.com/questions/8551940/mysql-insert-datetime-into-other-datetimefield引起的語法錯誤不使用單引號。另請參閱官方文檔。 http://dev.mysql.com/doc/refman/5.1/en/date-and-time-literals.html另一個需要單引號的引用:http://stackoverflow.com/questions/5452124/inserting-整數和日期時間從PHP到MySQL 5-0-DB錯誤 – 2012-02-15 18:43:46

+0

@Jonathan - 這與日期*字符串*。 'createODBCDate'返回一個不同處理的日期時間*對象*。日期時間對象不需要單引號(實際上添加它們會導致語法錯誤)。 – Leigh 2012-02-15 19:04:41