2009-09-24 146 views
0

我有一个经典的ASP页面,其中包含一些代码以检查表中是否存在电子邮件,如下所示;检查电子邮件是否存在

<% 
    '' //Check the submitted email against existing ones in the database 
    set CmdCheckEmail = server.CreateObject("ADODB.Command") 
    CmdCheckEmail.ActiveConnection = MM_dbconn_STRING 
    CmdCheckEmail.CommandText = "SELECT COUNT(ReferredEmail) AS 'CountEmail' FROM TenantReferral WHERE ReferredEmail = '" & Request("Email") & "'" 
    Response.Write(CmdCheckEmail.CommandText) 
    CmdCheckEmail.CommandType = 1 
    CmdCheckEmail.CommandTimeout = 0 
    CmdCheckEmail.Prepared = true 
    CmdCheckEmail.Execute() 

    countEmail = CmdCheckEmail("CountEmail") 

    set CmdCheckEmail = nothing 
    conn.close 
    set conn = nothing 

    If(countEmail >= 1) Then 
     Message = Message & "<p>This email address has already been referred.</p>" 
    End If 
%> 

但是,页面正在报告以下错误;

SELECT COUNT(ReferredEmail) AS 'CountEmail' FROM TenantReferral WHERE ReferredEmail = '[email protected]' 

ADODB.Command error '800a0cc1' 

Item cannot be found in the collection corresponding to the requested name or ordinal. 

/default2.asp, line 19 

第19行如下;

countEmail = CmdCheckEmail("CountEmail") 

电子邮件确实存在于表与表只是有以下几列; ReferredEmail和ReferredCode

我想知道是否有人能够解决这个错误?

谢谢。

回答

0

注意确保您正在使用的数据库,但试图改变你的SQL语句:

SELECT COUNT(ReferredEmail) AS CountEmail FROM TenantReferral WHERE ReferredEmail = '[email protected]' 

然后改变

CmdCheckEmail.Execute()  
countEmail = CmdCheckEmail("CountEmail") 

set rs = CmdCheckEmail.Execute() 
countEmail = rs("CountEmail") 

而且,你有一个SQL注入问题与该查询。你应该使用parameterized queries

+0

@Orbman - 我正在使用MSSQL数据库,但是仍然报告使用'CountEmail'或CountEmail时出现同样的错误:( – doubleplusgood 2009-09-24 11:44:27

+0

您正在使用cmd对象不正确,请参阅我的编辑 – RedFilter 2009-09-24 11:48:30

+0

感谢Orbman,我现在得到一个对象:'conn' /default2.asp,第20行错误 – doubleplusgood 2009-09-24 11:53:31

0

CmdCheckEmail("CountEmail")尝试访问Command对象的默认成员,该对象是参数集合。但是,您不想访问参数,而是访问生成的记录集的字段。

试试这个(未测试):

Set rs=CmdCheckEmail.Execute() 

countEmail = rs("CountEmail") 

除此之外,请注意:这条线:

CmdCheckEmail.CommandText = "SELECT COUNT(ReferredEmail) AS 'CountEmail' FROM TenantReferral WHERE ReferredEmail = '" & Request("Email") & "'" 

是容易受到攻击SQL injection

从不将字符串嵌入到SQL语句中;改用参数。 (在这种情况下,你可以使用Command.Parameters集合来做到这一点。)