Lucee 5.2,SQL Server 2014
我有一个表单,不会在我的SQL Server 2014数据库中插入新记录。
我已经研究了这个问题几天几夜,无法弄清楚发生了什么。我没有收到任何类型的错误消息。我填写表单,单击“提交”按钮,然后返回到空白表单。没有记录插入数据库表。
我已在下面测试了我的代码的删节版本,但我仍然得到相同的结果:没有插入;并且Lucee根本没有给我任何错误信息。
<!--- set a default value "" for RegisterID in scope URL --->
<cfparam name="url.RegisterID" default="">
<!--- define the RegisterID in scope FORM, then set form.RegisterID equal to the RegisterID passed in the URL --->
<cfparam name="form.RegisterID" default="#url.RegisterID#">
<!--- set default values for other user-editable fields --->
<cfparam name="form.Title" default="">
<cfparam name="form.x_first_name" default="">
<cfparam name="form.DateCreated" default="">
<cfparam name="form.DateModified" default="">
<!--- query editRegister tells Lucee which record to edit--->
<cfquery datasource="nnvawi" name="editRegister">
SELECT RegisterID
,Title
,x_first_name
,DateCreated
FROM NNVAWIRegister
WHERE RegisterID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.RegisterID)#">
</cfquery>
<cftry>
<cfset variables.error = "">
<cfif IsDefined("FORM.doSave")>
<!--- when a RegisterID Exists, the action is UPDATE --->
<cfif val(form.RegisterID)>
<!--- query UpdateUser updates a page record in content table --->
<cfquery name="UpdateUser" datasource="nnvawi">
UPDATE NNVAWIRegister
SET
Title = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(Left(form.Title,50))#">,
x_first_name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(Left(form.x_first_name,255))#">,
DateModified = <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">
WHERE RegisterID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.RegisterID)#">
</cfquery>
<!--- CFELSE: if RegisterID does not exist, then create new Page --->
<cfelse>
<!--- query to insert new user record into content table --->
<cfquery name="InsertRecord" datasource="nnvawi" result="newRegistrant">
INSERT INTO NNVAWIRegister
(
,Title
,x_first_name
,DateCreated
)
VALUES(
<cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(Left(form.Title,50))#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(Left(form.x_first_name,255))#">,
<cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">
)
</cfquery>
<!--- use the result attribute value (newRegistrant) to set form field value --->
<cfset form.RegisterID = newRegistrant.IDENTITYCOL>
<!--- END cfif val(form.RegisterID) -- if a record needed to be updated or added, then it was done --->
</cfif>
</cfif>
<cfcatch type="Any">
<cfset variables.error = cfcatch.message>
</cfcatch>
</cftry>
<cfif len(variables.error) eq '0'>
<cfloop index="aCol" list="#editRegister.columnList#">
<cfset "form.#aCol#" = editRegister[aCol][editRegister.currentRow]>
</cfloop>
</cfif>
<cfinclude template="/admin/admin_header.cfm">
<!--- if there an error, display error in readable form --->
<cfif len(variables.error)>
<cfoutput>
<div class="errorbox">#variables.error#</div>
</cfoutput>
<br />
<div class="align-center">
<input type=button value="Go Back" onClick="history.go(-1)">
</div>
<cfabort>
</cfif>
<cfparam name="url.cftoken" default="">
<cfif len(url.cftoken)>
<div class="center"><button class="medium green"><i class="fa fa-check-square"></i> Update succeeded. Good work!</button></div>
</cfif>
<cfoutput>
<form method="post" name="ebwebworkForm" class="ebwebworkForm">
<ul>
<li>
<legend><h2>Registration Details</h2></legend>
</li>
<input type="hidden" name="RegisterID" value="#form.RegisterID#" /><!--- Embed RegisterID (PK) to assign a value to it --->
<li>
<label for="Title"><h3>Title (Ms., Mr., Dr. etc.):</h3></label>
<input type="text" name="Title" placeholder="Title" value="#Trim(Left(form.Title,255))#" maxlength="255" tabindex="1" size="70" autofocus="true" />
</li>
<li>
<label for="x_first_name"><h3>First Name:</h3></label>
<input type="text" name="x_first_name" placeholder="First Name" value="#Trim(Left(form.x_first_name,255))#" maxlength="255" tabindex="2" size="70" required="yes" />
<span class="form_hint">Enter First Name</span>
</li>
<li>
<div class="submitButton">
<button name="doSave" type="submit" class="green" tabindex="19">Save Record</button>
</div>
</li>
</ul>
</form>
</cfoutput>
答案 0 :(得分:6)
您的INSERT SQL无效。值列表以逗号开头,表示Title
前面不能有逗号。
您没有看到错误消息的原因是查询被包装到<cftry>
/ <cfcatch>
块中。 <cfcatch>
内有<cfset variables.error = cfcatch.message>
,但实际的错误消息存储在cfcatch.detail
中。所以它不是输出。