Inserting a Request.Form variable into a SQL statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tnspc
    New Member
    • Feb 2007
    • 30

    #1

    Inserting a Request.Form variable into a SQL statement

    I'm trying to access a Request.Form variable and plug it into an SQL statement, so that the statement will select just the particular data I need.

    Here's the offending snippet of code:

    Code:
    sql = "SELECT DISTINCT name, extension FROM Employees WHERE name = " & Request.Form("name")
    I assumed that the problem was simply not having quotation marks in the right places, but I've tried many combinations of quotes, to no avail.

    Here's the complete code (keep in mind that I'm still testing it, so it may not entirely make sense; first, I tried to get it to output something, now I'm at the point of trying to narrow down the output to just what I actually need).

    Code:
    <%@ LANGUAGE = "VBSCRIPT"	%>
    <% Option Explicit %>
    
    <% If Request.Form("name") <> "" And Request.Form("extension") <> "" Then %> 
    <%
    	On Error Resume Next
    	Dim MyConnection, sql, rsRecords, results
    
    	sql = "SELECT DISTINCT name, extension FROM Employees WHERE name = " & Request.Form("name") 
    
    	Set MyConnection = Server.CreateObject("ADODB.Connection")
    	Set rsRecords = Server.CreateObject("ADODB.RecordSet")
    	
    	MyConnection.Open("employee")
    	rsRecords.Open sql, MyConnection 
    %>
    
    <% If rsRecords.EOF Then %>
    <H2> No records found </H2>
    <%End If %>
    
    
    <%
    
    While Not rsRecords.EOF
    results = rsRecords("name") & "&nbsp;&nbsp" & rsRecords("extension") & "<BR>"
    Response.Write(results)
    rsRecords.MoveNext
    WEND
    
    rsRecords.Close
    MyConnection.Close
    Set MyConnection = Nothing
    
    %>
    
    <%
    Else
    %>
    
    <HTML>
    <HEAD>
    <TITLE>Database Connection</TITLE>
    </HEAD>
    
    <BODY onLoad = "document.searchDB.name.focus()">
    <H3> Please Enter the Name and Extension You're Searching For: </H3>
    <TABLE>
    <FORM NAME = "searchDB"	METHOD = "post" ACTION = "ASPDBtest.asp">
    <TR><TD ALIGN = "left"><B>Name</B>
    <TD><INPUT TEXT = "text"  NAME = "name"  SIZE = "25">
    <TR><TD ALIGN = "left"><B>Extension</B>
    <TD><INPUT TEXT = "text"  NAME = "extension"  SIZE = "25">
    <TR><TD COLSPAN = "2"	ALIGN = "left">
    <TD><INPUT TYPE = "Submit"  VALUE = "Submit">
    <TD><INPUT TYPE = "Reset"  VALUE = "Reset">
    </FORM>
    </TABLE>
    
    </BODY>
    </HTML>
    
    <%End If%>
    AGAIN, please understand that this is still developmental code. Nothing is worse than getting back info on the rest of the program, when all I need (for this post anyway) is how to make that sql statement execute properly! :o) Help is greatly appreciated.
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    You say you have tried several combinations of quotes. The correct one should be:
    Code:
    sql = "SELECT DISTINCT name, extension FROM Employees WHERE name = '" & Request.Form("name") & "'"
    'print sql in HTML comment for troubleshooting purposes
    response.write "<!-- sql: " & sql & " -->" & vbNewLine
    Have you tried this one?

    Let me know if this works.

    Jared

    Comment

    • Arnold Schuur
      New Member
      • Apr 2007
      • 36

      #3
      If name is a char-field (which is likely) then the query must have single quotes ('):
      Code:
      sql = "SELECT DISTINCT name, extension FROM Employees WHERE name = '" & Request.Form("name") & "'"
      When you open the connection with conn.open you should supply the connectionstrin g. For example:

      Code:
      MyConnection.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
                        "DBQ=nwind.mdb;" & _
                        "DefaultDir=C:\program files\devstudio\vb;" & _
                        "Uid=Admin;Pwd=;"

      Comment

      • tnspc
        New Member
        • Feb 2007
        • 30

        #4
        Originally posted by jhardman
        You say you have tried several combinations of quotes. The correct one should be:
        Code:
        sql = "SELECT DISTINCT name, extension FROM Employees WHERE name = '" & Request.Form("name") & "'"
        'print sql in HTML comment for troubleshooting purposes
        response.write "<!-- sql: " & sql & " -->" & vbNewLine
        Have you tried this one?

        Let me know if this works.

        Jared


        It worked beautifully! One other quick question- what is the ASP version of a wildcard character, where the user could enter something close to the right name and get the person they're looking for?

        Comment

        • Arnold Schuur
          New Member
          • Apr 2007
          • 36

          #5
          Originally posted by tnspc
          what is the ASP version of a wildcard character, where the user could enter something close to the right name and get the person they're looking for?
          Code:
          Select * from Employees where name like '%John%'

          Comment

          • tnspc
            New Member
            • Feb 2007
            • 30

            #6
            Originally posted by Arnold Schuur
            Code:
            Select * from Employees where name like '%John%'

            Right, I know the % wildcard in SQL. How would I insert that into my previous code so that it would interpret properly inside the ASP delimiters?

            Comment

            • jhardman
              Recognized Expert Specialist
              • Jan 2007
              • 3405

              #7
              As Arnold said, it looks like you want the sql wildcard in this case. So try this:

              Code:
              sql = "SELECT DISTINCT name, extension FROM Employees WHERE name LIKE '%" & Request.Form("name") & "%'"
              Let me know how it goes.

              Jared

              Comment

              • tnspc
                New Member
                • Feb 2007
                • 30

                #8
                The wildcard character did not work. Again, probably an issue with the placement of quotes. However, I did have one other question: how do I get the SQL statement to search correctly if the name has an apostrophe in the record (ex. Jane O' Hare), and then get it to display right? I know there's a way to do it with escape characters, but I'm blanking on how to implement it. Otherwise the search is executing fine.

                p.s. If you have any more suggestions on the wildcard, please let me know

                Comment

                • jhardman
                  Recognized Expert Specialist
                  • Jan 2007
                  • 3405

                  #9
                  Replacing the ' with '' might work.
                  Code:
                  sql = replace(sql, "'", "''")
                  Jared

                  Comment

                  Working...