我现在已经为此工作了大约6个小时......我一点也不知道出了什么问题。我有一个各种购物车,每个项目都有表格变量来更新他们的数量。无论出于何种原因,完全随机,ColdFusion将为表单更新插入两条记录。我甚至删除了更新,并在insert语句上方放置了一个“DELETE ALL WHERE”查询,以确保从数据库中清除所有内容,没有运气。这是我的代码(恢复到更新语句):
<cfquery datasource="something" result="UpdateProdRecordBase">
Update dbo.CS
SET dbo.CS.Quantity = <cfqueryparam value="#form['base#ConfigurationList.The_Product_ID#group#CurrentProductReceiveGroup#']#" cfsqltype="cf_sql_numeric">
WHERE
dbo.CS.The_Product_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ConfigurationList.The_Product_ID#">
AND dbo.CS.The_TRequestSubSet_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#TRSS#">
AND dbo.CS.ConfigType = 1
AND dbo.CS.Group = <cfqueryparam value="#CurrentProductReceiveGroup#" cfsqltype="cf_sql_numeric">
AND dbo.CS.The_ParentProduct_ID = 0
</cfquery>
<cfif (isdefined("UpdateProdRecordBase.recordcount") AND UpdateProdRecordBase.recordcount EQ 0) OR (not isdefined("UpdateProdRecordBase.recordcount"))>
INSERT INTO dbo.CS(The_TRequestSubSet_ID, The_Product_ID, Quantity, ConfigType, Group)
VALUES (<cfqueryparam value="#TRSS#" cfsqltype="cf_sql_numeric">,
<cfqueryparam cfsqltype="cf_sql_numeric" value="#ConfigurationList.The_Product_ID#">,
<cfqueryparam value="#form ['base#ConfigurationList.The_Product_ID#group#CurrentProductReceiveGroup#']#" cfsqltype="cf_sql_numeric">,
1,<cfqueryparam value="#CurrentProductReceiveGroup#" cfsqltype="cf_sql_numeric">)
同样,这些是背靠背查询... 99.9%的时间它完美运作。另外.1%的时间,它会复制条目。这没有逻辑上的一致性。我还删除了过去的更新脚本,并在插入查询上方添加了删除查询。同样的运气。
为了保持一致,我多次测试了相同的产品组合。您选择产品,一旦时间,完美的工作。再次选择相同的产品组合,插入重复。
有什么想法?此时我完全没有选择。我在这个过程中输出了查询,没有明显的项目使这种情况发生,数据完全相同。
答案 0 :(得分:1)
我发现在我的所有插入之前添加if not exists
可以防止因各种原因造成的所有重复(最常见的是用户将提交按钮混搭lol。试试这个:
<cfquery>
if not exists (select The_TRequestSubSet_ID
from dbo.cs
WHERE The_TRequestSubSet_ID = <cfqueryparam value="#TRSS#" cfsqltype="cf_sql_numeric">
AND The_Product_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ConfigurationList.The_Product_ID#">
AND ConfigType = <cfqueryparam value="#form ['base#ConfigurationList.The_Product_ID#group#CurrentProductReceiveGroup#']#" cfsqltype="cf_sql_numeric">
AND Group = <cfqueryparam value="#CurrentProductReceiveGroup#" cfsqltype="cf_sql_numeric">
)
INSERT INTO dbo.CS(The_TRequestSubSet_ID , The_Product_ID, Quantity, ConfigType, Group)
VALUES (<cfqueryparam value="#TRSS#" cfsqltype="cf_sql_numeric">,
<cfqueryparam cfsqltype="cf_sql_numeric" value="#ConfigurationList.The_Product_ID#">,
<cfqueryparam value="#form ['base#ConfigurationList.The_Product_ID#group#CurrentProductReceiveGroup#']#" cfsqltype="cf_sql_numeric">,
1,<cfqueryparam value="#CurrentProductReceiveGroup#" cfsqltype="cf_sql_numeric">)
</cfquery>
这告诉SQL只插入记录(如果它尚不存在)。如果你真的想让事情变得更容易:
<cfquery>
if not exists (select The_TRequestSubSet_ID
from dbo.cs
WHERE The_TRequestSubSet_ID = <cfqueryparam value="#TRSS#" cfsqltype="cf_sql_numeric">
AND The_Product_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ConfigurationList.The_Product_ID#">
AND ConfigType = <cfqueryparam value="#form ['base#ConfigurationList.The_Product_ID#group#CurrentProductReceiveGroup#']#" cfsqltype="cf_sql_numeric">
AND Group = <cfqueryparam value="#CurrentProductReceiveGroup#" cfsqltype="cf_sql_numeric">
)
begin
INSERT INTO dbo.CS(The_TRequestSubSet_ID, The_Product_ID, Quantity, ConfigType, Group)
VALUES (<cfqueryparam value="#TRSS#" cfsqltype="cf_sql_numeric">,
<cfqueryparam cfsqltype="cf_sql_numeric" value="#ConfigurationList.The_Product_ID#">,
<cfqueryparam value="#form ['base#ConfigurationList.The_Product_ID#group#CurrentProductReceiveGroup#']#" cfsqltype="cf_sql_numeric">,
1,<cfqueryparam value="#CurrentProductReceiveGroup#" cfsqltype="cf_sql_numeric">)
end
else
begin
Update dbo.CS
SET dbo.CS.Quantity = <cfqueryparam value="#form['base#ConfigurationList.The_Product_ID#group#CurrentProductReceiveGroup#']#" cfsqltype="cf_sql_numeric">
WHERE
dbo.CS.The_Product_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ConfigurationList.The_Product_ID#">
AND dbo.CS.The_TRequestSubSet_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#TRSS#">
AND dbo.CS.ConfigType = 1
AND dbo.CS.Group = <cfqueryparam value="#CurrentProductReceiveGroup#" cfsqltype="cf_sql_numeric">
AND dbo.CS.The_ParentProduct_ID = 0
End
</cfquery>
现在您有一个查询insert
如果不存在,或者update
如果
答案 1 :(得分:1)
您的页面可能已经提交了两次,就像其他人已经说过http日志会确认这一点,但正确处理双重帖子仍然很重要。
我使用的策略是在会话中记录帖子,然后拒绝重复。
发送页面:
<form ...>
<input type="hidden" name="magicnumber" value="#CreateUUID()#">
</form>
接收页面:
<cflock scope="session" type="exclusive">
<cfif StructKeyExists(session.magicnumbers, form.magicnumber)>
<cfset variables.isdup = true>
<cfelse>
<cfset session.magicnumbers[form.magicnumber] = true>
</cfif>
</cflock>
<cfif !isdup>
...
</cfif>
然后我还会使用303标题重定向到另一个页面以显示任何内容。
当然,您必须提前创建session.magicnumbers,例如OnSessionStart() 如果您不使用用户会话,则可以使用应用程序或服务器范围,但计划以某种方式写入以删除旧值,这样您就不会非常慢慢地记忆。