Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


If ... Then ... Else statement

If ... Then ... Else statement

If ... Then ... Else statement

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("ip") <> strIP Then %>

what's wrong?

RE: If ... Then ... Else statement

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("ip") = 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.

Dim foundFlag
foundFlag = false
Do Until rs.EOF
   If rs("ip") = strIP Then foundFlag = true

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:
Dim foundFlag
foundFlag = false
Do Until rs.EOF
   If rs("ip") = strIP Then
      foundFlag = true
      Exit Loop 'check syntax here, I am a little sleepy
   End If

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:

rsip = "SELECT * FROM ip WHERE ip = '" & strIP & "'"

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:

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 "Sorry you have already answered this question"
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:

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 :)


Want to get great answers to your Tek-Tips questions? Have a look at FAQ333-2924

RE: If ... Then ... Else statement

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 "polls.asp?Id="& Id %>
but i want to pass another value on the redirect, how do i do it?

RE: If ... Then ... Else statement

Tarwn please take a look at this code here and please tell me what's wrong(if anything)

dim rs, rsip
set conn = Server.CreateObject("adodb.connection")
conn.open "opiniao"
set rs = conn.execute (rsip)
rsip = "select ip from ip WHERE ip = '" & strIP & "'"

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

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


conn.execute sql
conn.execute sqlIP
<% set rs = nothing
set conn = nothing

It's not complete i'm developing it(with your help)

RE: If ... Then ... Else statement

To add asnother querystring field/value pair just do this:
Response.Redirect "polls.asp?Id=" & id & "&field2=value2"

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

You've also misplaced your recordset here:

set rs = conn.execute (rsip)
rsip = "select ip from ip WHERE ip = '" & strIP & "'"

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


Want to get great answers to your Tek-Tips questions? Have a look at FAQ333-2924

RE: If ... Then ... Else statement

i've made the chages you said but it keeps on accepting the vote even if the IP exists!!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close