Simple IF Statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Simple IF Statement

Post by nickman013 »

Hello,

I have a page that I want to block out users if their IP is in a row on one of my tables in a database I have.

My table has rows with just IP's and they get added when they vote on a voting page I made.

Now I need help creating a IF statement that does somthing like

IF their IP is in one of the rows , then echo "you already voted" and IF they didnt vote, then it will continue with the rest of my page...

THANKS ALOT!!!!!!!!!!
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

You would use a select query with a where clause that checks the user's IP address ($_SERVER['REMOTE_ADDR']) against rows in the table. Use mysql_num_rows on the result, and if this returns a positive number (a row exists in the database for this IP address), then show your "already voted" message
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thanks for responding so quickly :D .

But,

I need a little bit of help...

I got the query correctly (I think :roll: ).

Code: Select all

$sql4 = "SELECT * FROM `moty_ip` WHERE `IP` =".$ip;
Now I just dont understand how to do the mysql_num_rows . and the other part. Can you possibly help me? I dont know mysql that good as I know PHP so little..

Thanks alot!!!!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Firstly you need to quote the ip address, considering it is a string.

Secondly, have you read mysql_num_rows()?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Code: Select all

<?php
$host = "localhost";//Hostname
$usr = "root";//DB Usr
$psw = "";//DB Password
$dbname = "ipdb";//DB Name
$tbl_name = "iptab";//Table Name
$ip_field = "ip";//Field For IP
$visitor_ip = $_SERVER[REMOTE_ADDR];//Visitor's IP Address

$conn = mysql_connect($host, $usr, $psw) or die(mysql_error());
mysql_select_db($dbname, $conn) or die(mysql_error();
$sql = "SELECT $ip_field FROM $tbl_name WHERE $ip_field = $visitor_ip";
$res = mysql_query($sql, $conn) or die(mysql_error());
$num_rows = mysql_num_rows($res);
if($num_rows == 1)
  {
    echo "Hello You Are Already Voted";
  }
else
  {
    //Your Site Codes Would Be Here
  }
?>
Last edited by neel_basu on Sun Dec 10, 2006 12:07 pm, edited 1 time in total.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Firstly, you need to relax..

Secondly.. I tried, and this is how far I got... and now I get a error, my code is...

Code: Select all

<?
$ip = @$REMOTE_ADDR;
$username2= "muot_report";  
$password2= "password";  
$database2= "muot_report";  
$connection2 = mysql_connect('localhost',$username2,$password2);  
mysql_select_db($database2); 

$result = "SELECT * FROM `moty_ip` WHERE `IP` =".$ip;
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
?>
and the error is...
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/muot/public_html/moty_test.php on line 10
Row 10:

Code: Select all

$num_rows = mysql_num_rows($result);
Thanks!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

nickman013 wrote:Firstly, you need to relax..
Who needs to relax?
nickman013 wrote: Secondly.. I tried, and this is how far I got... and now I get a error, my code is...

Code: Select all

<?
$ip = @$REMOTE_ADDR;
$username2= "muot_report";  
$password2= "password";  
$database2= "muot_report";  
$connection2 = mysql_connect('localhost',$username2,$password2);  
mysql_select_db($database2); 

$result = "SELECT * FROM `moty_ip` WHERE `IP` =".$ip;
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
?>
Again, you need to put single quotes around $ip because it is a string. Mysql will throw an error if you don't.

Now your getting the error because you havn't passed your query to mysql_query(), the result of that is what you pass the mysql num rows. Alternatively, you could do SELECT COUNT(*) as `count` .... and fetch the result as $row['count'].. this is often more efficient.

You might want to consider using $_SERVER['REMOTE_ADDR'], considering $REMOTE_ADDR is deprecated.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

The Above Code Will Work (All You Need To Set The Values Of The Variable)
=====================================================

Code: Select all

$host = "localhost";//Hostname 
$usr = "root";//DB Usr 
$psw = "";//DB Password 
$dbname = "ipdb";//DB Name 
$tbl_name = "iptab";//Table Name 
$ip_field = "ip";//Field For IP 
$visitor_ip = $_SERVER[REMOTE_ADDR];//Visitor's IP Address
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

I get a parse error

Error:
Parse error: parse error, unexpected ';' in /home/muot/public_html/moty_test.php on line 11

Line 11:
mysql_select_db($dbname, $conn) or die(mysql_error();


Code:

Code: Select all

<?php 
$host = "localhost";//Hostname 
$usr = "muot_report";//DB Usr 
$psw = "passssss";//DB Password 
$dbname = "muot_report";//DB Name 
$tbl_name = "moty_ip";//Table Name 
$ip_field = "IP";//Field For IP 
$visitor_ip = $_SERVER[REMOTE_ADDR];//Visitor's IP Address 

$conn = mysql_connect($host, $usr, $psw) or die(mysql_error()); 
mysql_select_db($dbname, $conn) or die(mysql_error(); 
$sql = "SELECT $ip_field FROM $tbl_name WHERE $ip_field = $visitor_ip"; 
$res = mysql_query($sql, $conn) or die(mysql_error()); 
$num_rows = mysql_num_rows($res); 
if($num_rows == 1) 
  { 
    echo "Hello You Are Already Voted"; 
  } 
else 
  { 
    //Your Site Codes Would Be Here 
  } 
?>
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Oh!!
Sorry That Would Be
mysql_select_db($dbname, $conn) or die(mysql_error());
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

My advice is obviously being ignored.. for that I am no longer participating in this thread. Good luck.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

If you check line 11, you're missing a closing parentheses. Also, I'm not sure if you're reading JCart's posts or not, but he's pointed out twice that you need single quotes around $visitor_ip or mysql will return an error.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thanks...

Now My Code

Code: Select all

<?php 
$host = "localhost";//Hostname 
$usr = "muot_report";//DB Usr 
$psw = "report";//DB Password 
$dbname = "muot_report";//DB Name 
$tbl_name = "moty_ip";//Table Name 
$ip_field = "IP";//Field For IP 
$visitor_ip = $_SERVER[REMOTE_ADDR];//Visitor's IP Address 

$conn = mysql_connect($host, $usr, $psw) or die(mysql_error()); 
mysql_select_db($dbname, $conn) or die(mysql_error());
$sql = "SELECT $ip_field FROM $tbl_name WHERE $ip_field = '$visitor_ip'"; 
$res = mysql_query($sql, $conn) or die(mysql_error()); 
$num_rows = mysql_num_rows($res); 
if($num_rows == 1) 
  { 
    echo "Hello You Are Already Voted"; 
  } 
else 
  { 
 ?>

rest of page HTML

<?
  } 
?>
My IP is in one of the rows, and I am not getting the hello you already voted message, I get the rest of the page HTML...
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

You could hardly call it your code, but have you checked that the IP was inserted into the table, and that it's the same as $visitor_ip?
Last edited by aaronhall on Sun Dec 10, 2006 12:33 pm, edited 2 times in total.
Post Reply