Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If ... Then ... Else statement 1

Status
Not open for further replies.

Herminio

Technical User
Joined
May 10, 2002
Messages
189
Location
PT
I have a variable that stores the user IP

strip = request.ServerVariables("REMOTE_ADDR")

Then i have to use the IF.. THEN... ELSE to see if that IP is on the DB

this is my If statement

<% If rs(&quot;ip&quot;) <> strIP Then %>

what's wrong?
 
I am assuming that your sql string for this is still: SELECT * FROM ip

With your if statement above your checking the first record returned in your redordset against the value of the variable strIP. Basically the only time this will be false ( rs(&quot;ip&quot;) = to strIP ) is when the very first person who visited your site comes back.

In order to fix this programmatically you would need to do a loop rather then an if-then-else stmt.

Code:
Dim foundFlag
foundFlag = false
Do Until rs.EOF
   If rs(&quot;ip&quot;) = strIP Then foundFlag = true
   rs.MoveNext
Loop[code]

Then you could do your if check against the foundFlag. If foundFlag is true than the loop fopund one in the process of moving throught the recordset, otherwise it didn't. To speed this up you could make it exit the loop upon finding a match:
[code]Dim foundFlag
foundFlag = false
Do Until rs.EOF
   If rs(&quot;ip&quot;) = strIP Then 
      foundFlag = true
      Exit Loop 'check syntax here, I am a little sleepy
   End If
   rs.MoveNext
Loop[code]
Now it only loops all the way through the recordset if it doesn't find a match, otherwise it sets the flag and jumps out after a single match is found.

Unfortunatly this is a little slow when it somes to processing, or rather, there is a much quicker way of doing it. If you allow the database to do the work for you, then you can take advantage of the fact that it has been optimized in search techniques. Another advantage is that less data will be transmitted between your script and the database. In fact, if we do this correctly, then only a single record will need to be returned at most, instead of the entire contents of the table.

Ok, so we wil want to change your sql statement like so:
[code]
rsip = &quot;SELECT * FROM ip WHERE ip = '&quot; & strIP & &quot;'&quot;

Now what this does for us is simplifies the check. In order to check all we have to do now is simply see if there are any records returned, if ther are then we do the update, grab the new id, and insert to add a vote record for the ip number in question:
Code:
If rs.EOF Then  'a recordset is only returned EOF if it is empty
   'execute your insert here
   'query for the newly added user
   'execute your update here
Else  'rs is not empty, so this ip is already in db
   Response.Write &quot;Sorry you have already answered this question&quot;
End If

And that should do it for you. One word of caution, you may want to change your variable names before continuing. For example, the name rsip to me makes me think recordfset, but you have it as an sql statement. You should try to come up with some consistent naming technique so that in the event someone else has to go in and check your code they will be able to pick it up eaily. Here is what I would suggest:
Code:
Original   New Name
rsip       sql_checkIP
sql        sql_addVote
sqlIP      sql_addIP
rs         rs_checkIP

I hope the above helps, and feel free to ignore the portion on variable naming, this was only a suggestion :)

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
thanks once again Tarwn, you've been of much help for me, and thanks for the sugsetion anout the variable names, feel free to sugest whatever things you think that can be of help, i appreciate it.

there's another thing id like to ask

on my if statement and if the rs does contains the IP i have to redirect the user to the previous page, i'm using a redirect like this

<% response.Redirect &quot;polls.asp?Id=&quot;& Id %>
but i want to pass another value on the redirect, how do i do it?
 
Tarwn please take a look at this code here and please tell me what's wrong(if anything)


<%@LANGUAGE=&quot;VBSCRIPT&quot; CODEPAGE=&quot;1252&quot;%>
<%
dim rs, rsip
set conn = Server.CreateObject(&quot;adodb.connection&quot;)
conn.open &quot;opiniao&quot;
set rs = conn.execute (rsip)
rsip = &quot;select ip from ip WHERE ip = '&quot; & strIP & &quot;'&quot;
%>

<%
Dim vote, Id, sql, sqlIP, strIP
vote = Request.Form(&quot;hip&quot;)
Id = Request.QueryString(&quot;Id&quot;)
strIP = request.ServerVariables(&quot;REMOTE_ADDR&quot;)
sql = &quot;update respostas&quot;
sqlIP = &quot;insert into IP (IdAssunto,IP) values ('&quot;& Id &&quot;','&quot;& strIP &&quot;')&quot;
%>
<% If rs.eof Then %>
<%
If vote = &quot;1&quot; Then
sql = sql & &quot; set resp1 = resp1 + 1,TotalVotos = TotalVotos + 1 where IdAssunto = &quot;&Id
ElseIf vote = &quot;2&quot; Then
sql = sql & &quot; set resp2 = resp2 + 1,TotalVotos = TotalVotos + 1 where IdAssunto = &quot;&Id
ElseIf vote = &quot;3&quot; Then
sql = sql & &quot; set resp3 = resp3 + 1,TotalVotos = TotalVotos + 1 where IdAssunto = &quot;&Id
End If
%>
<% Else %>
<% response.Redirect &quot;polls.asp?Id=&quot;& Id
%>
<% End If %>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>


</body>
</html>
<%
conn.execute sql
conn.execute sqlIP
%>
<% set rs = nothing
set conn = nothing
%>


It's not complete i'm developing it(with your help)
 
To add asnother querystring field/value pair just do this:
Response.Redirect &quot;polls.asp?Id=&quot; & id & &quot;&field2=value2&quot;

Basically you want it to look like this:
polls.asp?Id=123&field2=value2&field3=value3 etc.

You've also misplaced your recordset here:
Code:
set rs = conn.execute (rsip)
rsip = &quot;select ip from ip WHERE ip = '&quot; & strIP & &quot;'&quot;

It's going to be hard to execute that sql when it gets executed before you define it ;)

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
i've made the chages you said but it keeps on accepting the vote even if the IP exists!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top