0

你好呀。在我的表单上进行测试提交后,我在此语句中收到语法错误:

“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/a-pyc.html 中的语法错误(缺少运算符) http:// /anpsa.org.au/jpg/029_2.jpg ' AND price='$72' AND information='Golden Wattle 是澳大利亚的国花。''。

/courses/benv/2410/2013s2/3420384/exercises/ex05/insert-plant.asp,第 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
4

1 回答 1

1

Insert into 语句中缺少引号应该是:

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

同样在错误消息 desc "Australia's" 中,单引号将分隔字符串。要解决此问题,请加倍变量内的单引号数量。可以使用以下功能:

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)   
&"');"
于 2013-09-30T05:44:01.030 回答