2013-09-30 164 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/)。 –