Simple IF Statement
Moderator: General Moderators
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
Simple IF Statement
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!!!!!!!!!!
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!!!!!!!!!!
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
Thanks for responding so quickly
.
But,
I need a little bit of help...
I got the query correctly (I think
).
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!!!!
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;Thanks alot!!!!
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Firstly you need to quote the ip address, considering it is a string.
Secondly, have you read mysql_num_rows()?
Secondly, have you read mysql_num_rows()?
- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
You might also read this tutorial on conditional statements.
- neel_basu
- Forum Contributor
- Posts: 454
- Joined: Wed Dec 06, 2006 9:33 am
- Location: Picnic Garden, Kolkata, India
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.
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
Firstly, you need to relax..
Secondly.. I tried, and this is how far I got... and now I get a error, my code is...
and the error is...
Thanks!
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";
?>Row 10:Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/muot/public_html/moty_test.php on line 10
Code: Select all
$num_rows = mysql_num_rows($result);- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Who needs to relax?nickman013 wrote:Firstly, you need to relax..
Again, you need to put single quotes around $ip because it is a string. Mysql will throw an error if you don't.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"; ?>
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.
- neel_basu
- Forum Contributor
- Posts: 454
- Joined: Wed Dec 06, 2006 9:33 am
- Location: Picnic Garden, Kolkata, India
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- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
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:
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
}
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
Thanks...
Now My Code
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...
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
<?
}
?>- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
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.