Sql 有人能告诉我这句话有什么不对吗?

Sql 有人能告诉我这句话有什么不对吗?,sql,asp-classic,vbscript,Sql,Asp Classic,Vbscript,我用它在我的表中插入了一些东西,它一直给我这个错误: Microsoft VBScript compilation error '800a03ee' Expected ')' /thanks.asp, line 63 Set rstSimple = cnnSimple.Execute("insert into SALT (Email, Username, FirstName, LastName, ActivationCode) VALUES ('"Request.QueryString("pa

我用它在我的表中插入了一些东西,它一直给我这个错误:

Microsoft VBScript compilation error '800a03ee' Expected ')' /thanks.asp, line 63 Set rstSimple = cnnSimple.Execute("insert into SALT (Email, Username, FirstName, LastName, ActivationCode) VALUES ('"Request.QueryString("payer_email") & "', '" & Request.QueryString("payer_email") & "', '" & Request.QueryString("first_name") & "', '" & Request.QueryString("last_name") & "', '" & Request.QueryString("hash")"')) ---------------------------------------------------------------------------------------------------------------------^ 有人能帮我吗


谢谢

您这里有一个缺少的
&

VALUES ('"Request.QueryString("payer_email") & "'
应该是:

VALUES ('" & Request.QueryString("payer_email") & "'
Request.QueryString("hash") & "')")
即使在你陈述的最后一部分,你也有一个缺失的
&
和一个缺失的

应该是:

VALUES ('" & Request.QueryString("payer_email") & "'
Request.QueryString("hash") & "')")
因此,您可能需要尝试以下语句:

cnnSimple.Execute("insert into SALT (Email, Username, FirstName, LastName, ActivationCode) VALUES ('" & Request.QueryString("payer_email") & "', '" & Request.QueryString("payer_email") & "', '" & Request.QueryString("first_name") & "', '" & Request.QueryString("last_name") & "', '" & Request.QueryString("hash") & "')")

似乎存在与括号相关的语法错误。
该行末尾的2个括号看起来有点可疑。

缺少的符号和引号可能是您遇到的最小问题


看起来您并没有以任何方式清理字符串。字符串可能包含未转义的单引号。您可以接受SQL注入,因为您没有使用参数。

我建议按如下方式分解代码,使其可读性和可理解性更好:

Dim execSql
execSql = "insert into SALT (Email, Username, FirstName, LastName, ActivationCode)"
execSql = execSql & " VALUES ('"
execSql = execSql & Request.QueryString("payer_email") 
execSql = execSql & "', '" 
execSql = execSql & Request.QueryString("payer_email") 
execSql = execSql & "', '" 
execSql = execSql & Request.QueryString("first_name") 
execSql = execSql & "', '" 
execSql = execSql & Request.QueryString("last_name") 
execSql = execSql & "', '" 
execSql = execSql & Request.QueryString("hash")
execSql = execSql & "')"

Set rstSimple = cnnSimple.Execute(execSql)
键入时,我删除了字符串中的引号错误。现在,如果您收到新错误,它们的位置将变得更加明显。此外,代码的颜色使其可读性强,并且易于发现错误(取决于您使用的编辑器)


编辑SQL注入和安全性 正如其他人已经提到的,您的代码极易受到SQL注入攻击。即使没有攻击(即删除您的数据库)的意思,如果某人的名字是
d'Amour
(法语)或
in't Huys
(荷兰语),它也会失败,使页面崩溃。要避免这种情况,请不要尝试筛选代码,而是使用SQL命令和参数重写代码。很简单,您的代码就是这样:

Set dbCommand = Server.CreateObject("ADODB.Command")
Set dbCommand.ActiveConnection = cnnSimple
dbCommand.CommandType = adCmdText
dbCommand.CommandText = _
    "INSERT INTO SALT (Email, Username, FirstName, LastName, ActivationCode) " + _ 
    "VALUES (@email, @user, @firstname, @lastname, @code)"
With dbCommand.Parameters
    .Add("email", adVarChar, adParamInput, , Request.QueryString("payer_email"))
    .Add("user", adVarChar, adParamInput, , Request.QueryString("payer_email"))
    .Add("firstname", adVarChar, adParamInput, , Request.QueryString("first_name"))
    .Add("lastname", adVarChar, adParamInput, , Request.QueryString("last_name"))
    .Add("code", adVarChar, adParamInput, , Request.QueryString("hash"))
End With

Set rstSimple = dbCommand.Execute()
注意:请确保将常量
adVarChar
adParamInput
等替换为等效的数值


有关更多信息,请参见Google上的“SQL注入ASP”或“SQL准备语句经典ASP”,它应该会给您带来一些帮助。

Um,我在其中添加了与上面类似的&但现在它显示:Microsoft VBScript编译错误“800a0409”未终止字符串常量/Thank.ASP,第63行Set rstSimple=cnnSimple.Execute(“插入到SALT中”)(电子邮件、用户名、名字、姓氏、激活码)值(“&Request.QueryString(“payer_Email”)&“,”&Request.QueryString(“payer_Email”)&“,”&Request.QueryString(“first_name”)&“,”&Request.QueryString(“last_name”)&“,”&Request.QueryString(“last_name”)&“,”,“&Request.QueryString(“hash”))-----------------^ @ J-T-S:检查我的更新答案。你也缺少一个引号,正如在另一个答案中提到的@ Alvin Dasasar。J-T-S:有很多其他缺失的引文,在中间的任何地方(但是这个答案的最后陈述似乎是正确的)。检查我的答案(自我推销,哎哟!)了解如何使您的代码稍微不易出错并更容易发现错误。其他人展示了类似的方法以使您的代码可读;-)。我刚刚尝试了此答案的最后一个示例,现在它显示:Microsoft VBScript运行时错误'800a01a8'所需对象:'@j-t-s:可能在设置
cnnSimple
时连接有问题。仍然缺少
&
和结束引号(我知道,我也必须重新编辑我的答案;-)注:重复的电子邮件值是正确的(
UserName
Email
是相等的),如果不使用该值,值的数量将不再与字段的数量匹配。这与我的建议非常相似。我不懂语言,所以我相信你,这比我的答案好+1@Mark:您的建议非常好,但应用于ASP有一些缺点(而且它不会编译,请参见您答案下的我的评论)。对于经典ASP,编码时最好非常保守。(但从10年前开始,C、F和Java是我选择的语言:)我刚试过,但现在它说:预期语句结束/Thank.asp,第63行Dim execSql As String=“insert into SALT(Email,Username,FirstName,LastName,ActivationCode)”--------------我的
Dim
受VB影响,对不起。现在就删除它。@j-t-s:我知道现在是周末,但是有没有新的问题提出,或者现在一切都正常了?谢谢你的关注!:)我喜欢周末工作。但不幸的是,我仍在努力让它发挥作用。一点运气都没有。