2011-05-30 92 views
1

我正在用ASP創建我的第一個項目。這個項目只是使用註冊系統進行基本的登錄/註銷。我想知道如何修正下面的代碼,以便當用戶註冊到我的網站並輸入已經存在於我的數據庫中的重複用戶名時,它將重定向到所有字段仍填寫完畢但用戶名字段爲空的表單給出用戶已經存在的消息。ASP/MS訪問檢查,看看用戶名是否存在

下面是我的代碼:

<% 
Dim objShop 
set objShop=Server.CreateObject("ADODB.Recordset") 

objShop.ActiveConnection=shop_STRING 

objShop.Source = "SELECT * FROM Users WHERE UserName=" & Request.Form("regUsername") 
objShop.CursorType=0 
objShop.CursorLocation=2 
objShop.LockType=3 
objShop.Open 

if not (objShop.EOF) then 
    objShop.Source="Users" 
    objShop.CursorType=0 
    objShop.CursorLocation=2 
    objShop.LockType=3 
    objShop.Open 

     objShop.Addnew 
     objShop("FirstName")= Request.Form("regFirst") 
     objShop("LastName")= Request.Form("regLast") 
     objShop("StudentID")= Request.Form("regID") 
     objShop("EmailAddress")= Request.Form("regEmail") 
     objShop("UserName")= Request.Form("regUsername") 
     objShop("Password")= Request.Form("regPassword") 
     objShop("Address")= Request.Form("regAddress") 
     objShop("Suburb")= Request.Form("regSuburb") 
     objShop("Postcode")= Request.Form("regPostcode") 
     objShop("ContactNumber")= Request.Form("regContact") 
     objShop("CCCompany")= Request.Form("regCCCompany") 
     objShop("CCNumber")= Request.Form("regCCNumber") 
     objShop("CCExpiryMonth")= Request.Form("regCCExpMonth") 
     objShop("CCExpiryYear")= Request.Form("regExpYear") 
     objShop("CCCVCNumber")= Request.Form("regCVCNumber") 
     objShop.Update 

    objShop.Close 
    set objShop= nothing 
else 
    response.write("Username already exists") 
end if 
%> 

我收到的代碼錯誤是:

Microsoft JET Database Engine error '80040e10' 

No value given for one or more required parameters. 

/home/registration.asp, line 17 

,所以我決定在再次投入的Response.Write(「用戶名已經存在」)如果聲明之前添加的值,然後出現此錯誤:

Microsoft JET Database Engine error '80040e14' 

Syntax error (missing operator) in query expression 'UserName='. 

/home/registration.asp, line 17 

我不知道我會怎麼corr解決這個問題。任何幫助,將不勝感激!

+1

[SQL注入操場..](http://en.wikipedia.org/wiki/SQL_injection)在'objShop.Source = ...' – 2011-05-30 12:54:16

回答

1

你需要在你的輸入單引號在SQL表達式

objShop.Source = "SELECT * FROM Users WHERE UserName='" & _ 
        Request.Form("regUsername") & "'" 
+0

現在它說需要Microsoft VBScript運行時錯誤 '800a01a8' 對象:'' /home/registration.asp,line 13 – George 2011-05-30 12:33:37

1

考慮你當前邏輯的這個僞代碼版本。

if not (objShop.EOF) then 
    AddNew 
Else 
    display notice that user account already exists 

沒有(objShop.EOF)將是真正的,如果你有用戶表其中username匹配regUsername一個或多個記錄。

那麼你的意思是這樣的:

If we have previously stored one or more records for this UserName 
    add another record for this UserName 
Else (no matching record exists) 
    display notice that user account already exists 

顯然你不應該收集並存儲信用卡信息。

相關問題