Bay*_*ara 1 javascript forms coldfusion cfquery cfform
我不明白为什么我会收到此错误。我正在使用 cfform 将数据从一个 html 页面发送到下一个。见下文。我注意到的另一件事是 angle_changes 字符串的前两个字符被截断。它应该是“0a0a0a0a”,但错误消息中传递了“0a0a0a”。
以下是来自spatialforaging.cfm的相关html/Javascript代码:
<!---all of these get passed from a previous page using cfoutput, except for angle_changes--->
<cfform action="field_transition.cfm" method="post" name="field_form">
<cfinput type="hidden" id="angle_changes" name="angle_changes" value="">
<cfinput type="hidden" id="subject_id" name="subject_id" value=#subject_id#>
<cfinput type="hidden" id="times_switched_away" name="times_switched_away" value=#times_switched_away#>
<cfinput type="hidden" id="total_time_unfocused" name="total_time_unfocused" value=#total_time_unfocused#>
<cfinput type="hidden" id="completed_fields" name="completed_fields" value="">
</cfform>
Run Code Online (Sandbox Code Playgroud)
脚本
//these values get changed earlier in the script
document.getElementById("times_switched_away").value = times_switched_away;
document.getElementById("total_time_unfocused").value = total_time_unfocused;
document.getElementById("completed_fields").value = completed_fields.toString();
//angleChanges is an array containing integers which is created elsewhere in the script
var angleChangesFormString = document.getElementById("angle_changes").value;
//adding each angle change to a string to add to database
for (i=0; i < angleChanges.length; i++) {
angleChangesFormString += angleChanges[i].toString();
angleChangesFormString += "a";
}
angleChangesFormString = angleChangesFormString.replace("undefined","");
document.getElementById("angle_changes").value = angleChangesFormString;
//this does print the correct value of '0a0a0a0a' to alert
alert(document.getElementById("angle_changes").value);
document.getElementById("field_form").submit();
Run Code Online (Sandbox Code Playgroud)
以下是来自 field_transition.cfm 的查询
<cfquery datasource="exmind">
update dbo.sf
set completed_fields = #completed_fields#
where subject_id = #subject_id#
</cfquery>
<cfquery datasource="exmind">
update dbo.sf
set times_switched_away = #times_switched_away#, total_time_unfocused = #total_time_unfocused#
where subject_id = #subject_id#
</cfquery>
<!---the first two queries work fine and update the database properly--->
<!---but this one gives the error message below.--->
<!---I do not see a syntax error, it is formatted exactly like the two queries above which work fine--->
<cfquery datasource="exmind">
update dbo.sf
set angle_changes = #angle_changes#
where subject_id = #subject_id#
</cfquery>
Run Code Online (Sandbox Code Playgroud)
错误信息:
执行数据库查询时出错。
[Macromedia][SQLServer JDBC 驱动程序][SQLServer]“a0a0a0a”附近的语法不正确。
错误发生在 C:REDACTED/field_transition.cfm: 第 64 行
62:更新dbo.sf
63:设置angle_changes = #angle_changes#
64:其中 subject_id = #subject_id#
65:
66:
供应商错误代码 102
SQLSTATE HY000
SQL 更新 dbo.sf 设置 angle_changes = 0a0a0a0a 其中 subject_id = 523550
数据源 exmind
我认为问题是你没有使用<cfqueryparam>
.
您尝试更新的字段看起来像字符串字段。
<cfquery datasource="exmind">
update dbo.sf
set angle_changes = <cfqueryparam value="#angle_changes#" cfsqltype="cf_sql_varchar">
where subject_id = <cfqueryparam value="#angle_changes#" cfsqltype="cf_sql_integer">
</cfquery>
Run Code Online (Sandbox Code Playgroud)
当尝试更新 varchar/char/text 数据库字段时,查询应如下所示(带引号)set angle_changes = '0a0a0a0a'
。<cfqueryparam>
类似的东西以更好的方式。
<cfqueryparam>
通过防止 SQL 注入来提高代码的安全性。