2013-09-30 161 views
0

你好。我得到一个语法错误在此声明后,我做了一个测试我的形式提交:经典asp数据库插入问题

“Microsoft Jet数据库引擎错误‘80040E14’

查询表达式

语法错误(缺少运算符)“c_name ='Golden Wattle'AND sc_name ='Acacia pycnantha'AND url ='http://anpsa.org.au/a-pyc.html'AND image ='http://anpsa.org.au/jpg/029_2.jpg'AND price ='$ 72'AND information ='Golden Wattle is Australia's national floral emblem。'。

/courses/benv/2410/2013s2/3420384/Exercises/ex05/insert-plant.asp,line 54“

对于我的生活,我无法理解这里出了什么问题。

dim cn, sc, url, image, price, desc 


    cn=Request.Form("new_cn") 
    sc=Request.Form("new_sc") 
    url=Request.Form("new_url") 
    image=Request.Form("new_image") 
     price=Request.Form("new_price") 
     desc=Request.Form("new_desc") 

    '--- check to see whether there already are items of that name... 
    SQL="select ID from PlantTable where c_name='"& cn & "' AND sc_name='" & sc & "'"&_ 
     " AND url='"&url& "' AND image='"&image& "' AND price='"&price& "' AND information='"&desc& "' " 
    set info = conn.execute(SQL) 

    if info.eof then 
    '--- there is no plant of that name at present, so do the insert 
    SQL="insert into PlantTable (c_name, sc_name, url, image, price, information) values ('" & cn & "', "&sn&","&url&","&image&","&price&","&desc&")" 
    conn.execute(SQL) 
    response.write "Insertion completed." 
    else 
    '--- there is already a plant of that name... 
    response.write "Sorry, that Plant Name is already in the database." 
    end if 
+0

调试101:添加打印语句打印出来的T-SQL语句发送到数据库引擎... –

+0

@MitchWheat谢谢您的帮助。我打印出一条SQL语句发送到数据库引擎并发现一个错误。 sc变量被写为sn。我改变了这一点。然后我收到另一个错误: 'Microsoft JET数据库引擎错误'80040e14' INSERT INTO语句中的语法错误。 /courses/benv/2410/2013s2/3420384/exercises/ex05/insert-plant.asp,line 61' 我试着打印出声明,但至今没有运气找到错误。 – chap

+0

这种问题的典型解决方案:使用[参数化查询](http://stackoverflow.com/a/18619736/1630171)。 –

回答

1

引号中缺少插入语句应该是:

SQL="insert into PlantTable (c_name, sc_name, url, image, price, information) values 
('" & cn & "', '"&sn&"', '"&url&"', '"&image&"', '"&price&"', '"&desc&"');" 

此外,在错误信息说明“澳大利亚”有单引号,将分隔字符串。要解决这个问题,请在变量中加倍单引号。下面的函数可用于:

Public Function ReplaceSingleQuotes(varValue As Variant) As String 
    Const SINGLEQUOTE = "'" 

    ReplaceSingleQuotes = SINGLEQUOTE & _ 
         Replace(varValue, SINGLEQUOTE, SINGLEQUOTE & SINGLEQUOTE) & _ 
         SINGLEQUOTE 
End Function 

,并可以用作:

SQL="insert into PlantTable (c_name, sc_name, url, image, price, information) values 
('" & cn & "', '"&sn&"', '"&url&"', '"&image&"', '"&price&"', '"& ReplaceSingleQuotes(desc) 
&"');" 
+0

不要尝试创造一种[天生破碎的方法](http://xkcd.com/327/)。 –