经典ASP/VBScript-插入后偶尔会返回错误的id



我工作的公司正在举办一场筹款活动,并要求我制作一个小的网络应用程序,通过PayPal接受捐款。这听起来很简单,但截止日期很紧,所以我决定挖掘出10多年前在经典ASP/VBScript中创建的类似内容,并重新使用它(以及Access数据库(。但后来需求发生了变化,变得更加复杂(尽管截止日期相同(,长话短说,我最终得到了一个混乱的代码和一个似乎运行良好的应用程序。。。直到今天,用户报告在应用程序的"谢谢"页面上看到了错误的数据。我很清楚,在将记录插入数据库后的某个时刻,代码偶尔会返回错误的id,但我无法复制错误或在代码中找到问题,我希望有人能帮助我。

最简单地说,以下是应用程序的工作方式:

  1. 用户可以选择注册三个不同的活动,并且必须通过PayPal捐款才能参加他选择的活动。根据他报名参加的活动,他还可以做出各种额外的选择。因此,基本上,用户填写一个标准的html表单,后台有一堆jQuery的东西来帮助引导用户完成各种选择
  2. 一旦用户做出了所有必要的选择并点击了"提交"按钮,我就会使用jQuery向经典ASP/VBScript页面发送ajax调用,该页面将用户的数据插入Access数据库,并将新插入的记录的id作为json响应返回
  3. ajax成功函数获取新id,将其插入到隐藏的PayPal表单上名为"item_number"的字段中,然后将PayPal表格提交给PayPal。然后用户被带到PayPal进行支付
  4. 我设置了PayPal的即时支付通知功能,这样当用户付款时,就会向我们服务器上的侦听器发送通知。通知在查询字符串中包含item_number,侦听器获取item_number,返回Access数据库并更新该记录,以显示用户实际已付款
  5. 最后,PayPal将用户返回到我们服务器上的感谢页面。感谢页面会拾取item_number查询字符串,在Access数据库中查询与该id匹配的记录,并显示对用户注册内容的确认(这是"感谢"页面(

问题是,在步骤2中,数据插入数据库(数据插入正确(后,偶尔会返回错误的id,这反过来又会对其余步骤产生不利影响。另一个问题是,有时当PayPal将用户返回到"谢谢"页面时,它会两次附加item_number查询字符串,这似乎会在服务器上引发500错误。。。所以用户会得到一个500页而不是一个谢谢。起初,我以为可能有两个人同时注册,Access只是有点蹩脚,但我能够确认当时只有一个用户注册了。

我认为问题一定出在步骤2的代码中,但我完全错过了它

'Declare field names
Dim Email
Dim EventType
'Declare variable for the id of the inserted record
Dim currentId
Email = Request.Form("Email")
EventType = Request.Form("EventType")
'if EventType = tournament, then check if user already registered
If EventType = "tournament" Then
    Dim rscheckregistration
    Dim rscheckregistration_cmd
    Dim checkRegistration
    Dim hasAlreadyRegistered
    hasAlreadyRegistered = 0
    Set rscheckregistration_cmd = Server.CreateObject ("ADODB.Command")
    rscheckregistration_cmd.ActiveConnection = connregistration
    rscheckregistration_cmd.CommandText = "SELECT COUNT(*) AS Total FROM tournament WHERE EventType = 'tournament' AND Email = '" & Email & "'" 
    rscheckregistration_cmd.Prepared = true
    Set rscheckregistration = rscheckregistration_cmd.Execute
    checkRegistration = rscheckregistration("Total")
    rscheckregistration.Close
    Set rscheckregistration = Nothing
    if checkRegistration > 0 Then 
        hasAlreadyRegistered = 1
    end if
    'do not allow user to register more than once
    if hasAlreadyRegistered = 1 Then
        Response.ContentType = "application/json"
        Response.Write("{ ""type"":""Already Registered""}")
    end if
    if hasAlreadyRegistered = 0 Then
        Set rsregister = Server.CreateObject("ADODB.Recordset")
        rsregister.ActiveConnection = connregistration
        rsregister.Source = "SELECT * FROM tournament"
        rsregister.CursorType = 2
        rsregister.LockType = 3
        rsregister.CursorLocation = 3
        rsregister.Open()
        rsregister.AddNew
        rsregister.Fields("Email") = Email
        rsregister.Fields("EventType") = "tournament"
        'get the id of the record that was just inserted
        rsregister.Update
        bookmark = rsregister.absolutePosition
        rsregister.Requery
        rsregister.absolutePosition = bookmark
        currentId = rsregister("TournamentId")
        rsregister.Close
        Set rsregister = Nothing
        Response.ContentType = "application/json"
        Response.Write("{ ""type"":" & currentId & "}")
    end if
'handle EventType = raffle and and EventType = casino
Else 
    Set rsregister = Server.CreateObject("ADODB.Recordset")
    rsregister.ActiveConnection = connregistration
    rsregister.Source = "SELECT * FROM tournament"
    rsregister.CursorType = 2
    rsregister.LockType = 3
    rsregister.CursorLocation = 3
    rsregister.Open()
    'insert the record
    rsregister.AddNew
    rsregister.Fields("Email") = Email
    rsregister.Fields("EventType") = EventType
    'get the id of the newly inserted record
    rsregister.Update
    bookmark = rsregister.absolutePosition
    rsregister.Requery
    rsregister.absolutePosition = bookmark
    currentId = rsregister("TournamentId")
    rsregister.Close
    Set rsregister = Nothing
    Response.ContentType = "application/json"
    Response.Write("{ ""type"":" & currentId & "}")
End if

不确定为什么需要Recordset.Requery()才能获得TournamentId——这应该也能正常工作(取决于您的连接,但应该由OLEDB和ODBC支持(。

Set rsregister = Server.CreateObject("ADODB.Recordset")
rsregister.ActiveConnection = connregistration
rsregister.Source = "SELECT * FROM tournament"
rsregister.CursorType = 1 'adOpenKeyset
rsregister.LockType = 3 'adLockOptimistic
rsregister.Open()
'insert the record
rsregister.AddNew
rsregister.Fields("Email") = Email
rsregister.Fields("EventType") = EventType
'get the id of the newly inserted record
rsregister.Update
currentId = rsregister("TournamentId")
rsregister.Close
Set rsregister = Nothing
Response.ContentType = "application/json"
Response.Write("{ ""type"":" & currentId & "}")

最新更新