我具有应保存一些表单字段的功能。此功能能够处理多个插入行。到目前为止,我已经能够创建逻辑来处理此问题,但是到目前为止,一旦用户尝试保存记录,我就遇到了唯一的问题。服务器响应如下所示:
The system has attempted to use an undefined value, which usually indicates a programming error, either in your code or some system code. Null Pointers are another name for undefined values. coldfusion
起初,我一直在寻找并尝试找出函数中未定义/缺少的值,但到目前为止我仍无法检测到该问题。我接下来要做的就是注释cfquery
。之后,我收到消息Form successfully saved.
。这告诉我我的代码在cfquery
标记内。这是我的代码示例:
<cffunction name="saveRecords" access="remote" output="false" returnformat="JSON">
<cfargument name="formID" type="string" required="true" default="">
<cfargument name="status1" type="string" required="true" default="0">
<cfargument name="status2" type="string" required="true" default="0">
<cfargument name="status3" type="string" required="true" default="0">
<cfargument name="status4" type="string" required="true" default="0">
<cfargument name="status5" type="string" required="true" default="0">
<cfargument name="comment1" type="string" required="true" default="">
<cfargument name="comment2" type="string" required="true" default="">
<cfargument name="comment3" type="string" required="true" default="">
<cfargument name="comment4" type="string" required="true" default="">
<cfargument name="comment5" type="string" required="true" default="">
<cfset local.fnResults = structNew() />
<cfset local.runProcess = true />
<cfset local.arrStatus = arrayNew(1) />
<cfloop collection="#arguments#" item="i">
<cfif findNoCase(left(i,6), "status") and arguments[i] eq 1>
<cfset arrayAppend(local.arrStatus, right(i,1)) />
</cfif>
</cfloop>
<cfset local.frmValidation = {
formID : len(arguments.formID),
status1 : ArrayContains([0,1], trim(arguments.status1)),
status2 : ArrayContains([0,1], trim(arguments.status2)),
status3 : ArrayContains([0,1], trim(arguments.status3)),
status4 : ArrayContains([0,1], trim(arguments.status4)),
status5 : ArrayContains([0,1], trim(arguments.status5))
}/>
<cfloop collection="#frmValidation#" item="i">
<cfif !frmValidation[i] or !arrayLen(arrStatus)>
<cfset local.runProcess = false />
<cfset local.fnResults = {status: 400, message: "Form data is either missing or incorrect."}>
<cfbreak>
</cfif>
</cfloop>
<cfif runProcess>
<!---
<cfquery name="saveRec" datasource="testDB">
<cfloop array="#arrStatus#" index="i">
INSERT INTO formDetails (
formid,
refid,
status,
comment,
userid,
lastupdate
)
SELECT
<cfqueryparam cfsqltype="cf_sql_numeric" value="#trim(arguments.formID)#">,
#i#,
<cfqueryparam cfsqltype="cf_sql_bit" value="#trim(arguments["status"&i])#">,
<cfqueryparam cfsqltype="cf_sql_varchar" maxlength="8000" value="#trim(arguments["comment"&i])#" null="#!len(trim(arguments["comment"&i]))#">,
<cfqueryparam cfsqltype="cf_sql_varchar" maxlength="6" value="#trim(session.userid)#" null="#!len(trim(session.userid))#">,
#now()#
WHERE NOT EXISTS (
SELECT refid
FROM formDetails
WHERE formid = <cfqueryparam cfsqltype="cf_sql_numeric" value="#trim(arguments.formID)#">
AND refid = #i#
)
</cfloop>
</cfquery>
--->
<cfset local.fnResults = {status: 200, message: "Form successfully saved!"}>
</cfif>
<cfreturn fnResults>
</cffunction>
我忘记提到的一件事是,在我注释掉cfquery之前,我试图保存记录,并且会发生我上面提到的错误,但同时将记录保存在数据库中。这很奇怪,因为我没有期望由于错误而导致查询无法成功执行。我使用Sybase数据库,这是有关formDetails
表的详细信息:
column name data type length
recid numeric 18 PK
formid numeric 10 FK
refid numeric 18 FK
status bit 1
comment varchar 8000
userid varchar 6
lastupdate datetime 23
我不确定我的代码在哪里中断,为什么我收到关于undefined value
的错误消息。如果有人可以提供帮助或提供有关如何解决此问题的有用资源,请告诉我。
更新
我一直在寻找解决此问题的方法,并且尝试将cfscript与UNION ALL
一起使用。很少有人建议这样做,这是我的代码示例:
remote function saveRecords(required string formID, string status1="0", string status2="0", string status3="0", string status4="0", string status5="0", string comment1="", string comment2="", string comment3="", string comment4="", string comment5="") output=false returnFormat="JSON" {
local.fnResults = structNew();
local.runProcess = true;
local.arrReference = arrayNew(1);
try {
local.frmValidation = {
formID : len(arguments.formID),
status1 : ArrayContains([0,1], arguments.status1),
status2 : ArrayContains([0,1], arguments.status2),
status3 : ArrayContains([0,1], arguments.status3),
status4 : ArrayContains([0,1], arguments.status4),
status5 : ArrayContains([0,1], arguments.status5)
};
for ( i in frmValidation ) {
if ( !frmValidation[i] ) {
local.runProcess = false;
local.fnResults = {status: 400, message: "Form data is either missing or incorrect."};
break;
}
}
for ( fld in arguments ) {
if ( findNoCase(left(fld,6), "status") && arguments[fld] == 1 ) {
arrayAppend(arrReference, right(fld,1));
}
}
if ( runProcess ) {
local.qrySql = "INSERT INTO formDetails(recid, formid, refid, status, comment, userid, lastupdate)";
local.qryParams = [];
for ( idx=1 ; idx<=arraylen(arrReference) ; idx++ ) {
local.referenceID = arrReference[idx];
local.recPK = trim(arguments.formID) & trim(referenceID);
local.statusVal = trim(arguments["status" & local.referenceID]);
local.commentVal = trim(arguments["comment" & local.referenceID]);
if ( idx != 1 ) {
local.qrySql &= " UNION ALL ";
}
local.qrySql &= " SELECT ?,?,?,?,?,?,?";
qryParams.append({cfsqltype="cf_sql_numeric", value=local.recPK});
qryParams.append({cfsqltype="cf_sql_numeric", value=trim(arguments.formID)});
qryParams.append({cfsqltype="cf_sql_numeric", value=local.referenceID});
qryParams.append({cfsqltype="cf_sql_tinyint", value=local.statusVal});
qryParams.append({cfsqltype="cf_sql_varchar", value=local.commentVal, maxlength=8000, null=!len(local.commentVal)});
qryParams.append({cfsqltype="cf_sql_varchar", value=trim(session.userid), maxlength=6, null=!len(trim(session.userid))});
qryParams.append({cfsqltype="cf_sql_timestamp", value=now()});
}
local.qryResult = queryExecute(qrySQL, qryParams, {datasource="#application.datasource#", result="insertRecords"});
local.fnResults = {result: insertRecords, sql: qrySql, params: qryParams, array: arrReference, args: arguments, status: 200, message: "Form successfully saved!"};
}
} catch (any e) {
local.fnResults = {sql: qrySql, params: qryParams, error: e, status: 400, message: "Error! Something went wrong..."};
}
return fnResults;
}
尝试保存记录后,收到消息Something went wrong...
。我回想了一下,所看到的只是指向此行的错误:
local.qryResult = queryExecute(qrySQL, qryParams, {datasource="#application.datasource#", result="insertRecords"});
我检查了数据库,记录在那里。没有关于此错误的详细信息。至少我没有收到上面问题中描述的错误,但是仍然不知道是什么导致我的代码崩溃。