ASP Compare server variable with DB recordset

206 views Asked by At

I'm new to this old scripting language but it's all we have right now. I'm trying to get this code work.

I would like to compare USERID servervariable with the same USERID from a recordset, then if true it will redirect the name of that USERID.

<%

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=I:\storyData.mdb" 

set rs = Server.CreateObject("ADODB.Recordset")

rs.Open "SELECT USERIDFROM preprod", conn

strName = "John"
strNo = "This is not you"

If Request.ServerVariables("HTTP_USERID") = (rs.Fields.Item("USERID").Value) Then

Response.Redirect("story.html?" & "name=" & strName)

Else

Response.Redirect("story.html?" & "name=" & strNo)

End If

%>

I hope this makes any sense. It seems simple but can't get it to work.

thanks

1

There are 1 answers

14
Josh Montgomery On

what "doesn't" work?

looking at your code, it seems you're only checking the the first record of your query. I think you want to do something like this:

<%

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=I:\storyData.mdb" 

set rs = Server.CreateObject("ADODB.Recordset")

rs.Open "SELECT count( USERID ) FROM preprod where userID = " & Request.ServerVariables("HTTP_USERID"), conn

strName = "John"
strNo = "This is not you"

if CInt( rs( 0 ) ) > 0 then

    Response.Redirect("story.html?" & "name=" & strName)
Else

    Response.Redirect("story.html?" & "name=" & strNo)

End If

%>

this code does a count of userIDs in your table and if there are more than 1, it'll do the redirect properly. A lot more efficient this way.