Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

Join Tek-Tips
*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.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Herminio (TechnicalUser) (OP)
4 Jan 03 9:19
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?
Helpful Member!  Tarwn (Programmer)
4 Jan 03 10:39
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
   rs.MoveNext
Loop


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
   rs.MoveNext
Loop

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

-Tarwn

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

Herminio (TechnicalUser) (OP)
4 Jan 03 12:33
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?
Herminio (TechnicalUser) (OP)
4 Jan 03 12:46
Tarwn please take a look at this code here and please tell me what's wrong(if anything)


<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
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 %>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</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)
Tarwn (Programmer)
4 Jan 03 13:43
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 ;)

-Tarwn

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

Herminio (TechnicalUser) (OP)
4 Jan 03 13:53
i've made the chages you said but it keeps on accepting the vote even if the IP exists!!!

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!

Back To Forum

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