Page 1 of 2
Simple IF Statement
Posted: Sun Dec 10, 2006 11:37 am
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!!!!!!!!!!
Posted: Sun Dec 10, 2006 11:46 am
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
Posted: Sun Dec 10, 2006 11:53 am
by nickman013
Thanks for responding so quickly

.
But,
I need a little bit of help...
I got the query correctly (I think

).
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!!!!
Posted: Sun Dec 10, 2006 11:59 am
by John Cartwright
Firstly you need to quote the ip address, considering it is a string.
Secondly, have you read
mysql_num_rows()?
Posted: Sun Dec 10, 2006 12:04 pm
by aaronhall
Posted: Sun Dec 10, 2006 12:06 pm
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
}
?>
Posted: Sun Dec 10, 2006 12:06 pm
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!
Posted: Sun Dec 10, 2006 12:10 pm
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.
Posted: Sun Dec 10, 2006 12:11 pm
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
Posted: Sun Dec 10, 2006 12:17 pm
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
}
?>
Posted: Sun Dec 10, 2006 12:19 pm
by neel_basu
Oh!!
Sorry That Would Be
mysql_select_db($dbname, $conn) or die(mysql_error());
Posted: Sun Dec 10, 2006 12:20 pm
by John Cartwright
My advice is obviously being ignored.. for that I am no longer participating in this thread. Good luck.
Posted: Sun Dec 10, 2006 12:21 pm
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.
Posted: Sun Dec 10, 2006 12:27 pm
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...
Posted: Sun Dec 10, 2006 12:31 pm
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?