Connecting Tech Pros Worldwide Help | Site Map

SQL refuses to believe it works

Member
 
Join Date: Feb 2007
Posts: 51
#1: Mar 24 '09
How Do,

I have a rather simple website, which has some SQL embedded in the ASP to write user data to the underlying mdb database.

The first SQL statement looks up all the records in the database to help produce the required output which is a single string value which is the Customer's Order to be made in store for pickup. The second is to write the string into the database, and keeps shouting about a syntax error in the INSERT INTO statement. I however, cannot for the life of me find one.

I am more than aware that this is very poorly written and has absolutely no security features etc but they aren't necessary at the moment. All I need to know is how to get the Order in to the Database.

It is to be written to 'tblorder' into the fields 'username' and 'order' - both of which are text fields.

The database is properly shared and can be written to elsewhere on the site. The username comes from the username that is stored in the cookie 'UserName' and the product name from the recordset through indexing.

The String value is generated correctly, it just won't insert into the database.


Many Thanks,


NDayave


Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <head>
  4. <style>
  5. body{background: white)
  6. </style>
  7. <link rel="stylesheet" type="text/css" href="Main.css">
  8. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  9. <title>Our Products</title>
  10. </head>
  11.  
  12. <body>
  13. <%
  14. if request.cookies("UserName")="" then
  15.     response.write("<P align='Center'>You must be logged in to order from this page, please click <a target=Main href=login.asp>here</a> to log in, or click<a target=Main href=new_user.asp> here </a> if you are new to this site.</P>")
  16. else
  17. Dim Conn, Rs, sql
  18. Set Conn = Server.CreateObject("ADODB.Connection")
  19. Set Rs = Server.CreateObject("ADODB.Recordset")
  20. Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("HeyPesto.mdb")
  21. sql= "SELECT * FROM tblproducts;"
  22. Rs.Open sql, Conn
  23.  
  24. dim index, order
  25. index = 1
  26. order = ""
  27.  
  28.  
  29. do While not Rs.EOF
  30. If request.form("amount" & index) > 0 Then
  31.     order = order & request.cookies("UserName") & Rs("name") & request.form("amount" & index)
  32. end if
  33.  
  34. Rs.MoveNext
  35. index = index + 1
  36. loop
  37.  
  38.  
  39. Response.Write(order)
  40.  
  41.  
  42. Dim connorder, strConnorder, Rsorder
  43. Set connorder = Server.CreateObject("ADODB.Connection")
  44. strConnorder = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
  45. "Data Source=" & Server.MapPath("HeyPesto.mdb") & "; " & _
  46. "User ID=; Password="
  47. connorder.Open strConnorder
  48. Set Rsorder = connorder.Execute ("INSERT INTO tblorder(username,order) VALUES (" & request.cookies("UserName") & "','" & order & "')" )
  49.  
  50. Rs.Close
  51.  
  52. Set Rs = Nothing
  53. Set Conn = Nothing
  54.  
  55. Rsorder.Close
  56.  
  57. Set Rsorder = Nothing
  58. Set Connorder = Nothing
  59. end if
  60. %>
  61.  
  62. </body>
  63.  
  64. </html>
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#2: Mar 26 '09

re: SQL refuses to believe it works


you are missing a single quote mark:
Expand|Select|Wrap|Line Numbers
  1. Set Rsorder = connorder.Execute ("INSERT INTO tblorder(username,order) VALUES ('" & request.cookies("UserName") & "','" & order & "')" )
Please let me know if this helps.

Jared
Member
 
Join Date: Feb 2007
Posts: 51
#3: Mar 26 '09

re: SQL refuses to believe it works


Thanks for the suggestion.

Still throwing up error messages though:

Microsoft JET Database Engine error '80040e14'

Syntax error in INSERT INTO statement.

/Order.asp, line 49

NDayave
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#4: Mar 26 '09

re: SQL refuses to believe it works


in general, the best way to troubleshoot sql is to response.write the sql statement, then plug it in to a query analyzer. Did you say you were using sql server? there should be an analyzer built in to the management studio.
Member
 
Join Date: Feb 2007
Posts: 51
#5: Mar 26 '09

re: SQL refuses to believe it works


Turns out 'order' requires square brackets when it is a field name.

Thanks a lot for your input, it works fine now.

NDayave
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#6: Mar 27 '09

re: SQL refuses to believe it works


Ah yes, "order" is a sql reserved word. Any field name can be put in square brackets without bothering SQL, so some people do it always, the rest should make sure they never use reserved words (or words with reserved characters) for field names.
Reply

Tags
asp, database, error, sql, syntax


Similar ASP / Active Server Pages bytes