Question

I'm using Mysql_num_rows to fetch data (their email & IP), if it's already been input by said user then it will decline, but if it hasn't then it succeeds.

However, if I change my IP it still successes, when it shouldn't because im using the same email still? It's not checking both email and IP. Thankyew in advance '-'

$query = "SELECT * FROM tbl_1 WHERE Email='$Email' AND IP='$ip'";
$Result = mysql_query($query) or die ('unable to run: ' .mysql_error());
$count = mysql_num_rows($Result);

if($count==1) 
{
echo 'You have already entered this competition!';
}
else {
echo 'success';
}
Was it helpful?

Solution

Your code contains WHERE Email='$Email' AND IP='$ip'"; yet in a comment you wrote:

"$Email=$_POST['email']; from submission form, and the IP comes from code: $IP = $_SERVER["REMOTE_ADDR"]; which is at the top of this scripts page"

You're using $IP = $_SERVER["REMOTE_ADDR"]; where you should either be using

$ip = $_SERVER["REMOTE_ADDR"];

or WHERE Email='$Email' AND IP='$IP'";

$IP and $ip are NOT the same thing, they're "two different animals altogether" ---
Variables are case-sensitive.

Sidenote: You can use OR instead of AND that way, if someone tries to sign up with a different email but the same IP (and vice-versa), then they won't be able to.


Footnotes:

mysql_* functions deprecation notice:

http://www.php.net/manual/en/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.

Documentation for MySQL can be found at » http://dev.mysql.com/doc/.

OTHER TIPS

You can try like this, it's a little different

$query = "SELECT COUNT(*) as count Email, IP FROM tbl_1 WHERE Email='$Email' AND IP='$ip'";
$Result = mysql_query($query);
if($Result){
    $row = mysql_fetch_assoc($Result);
    if((int)$row['count'] > 0) 
    {
        echo 'You have already entered this competition!\n';
        echo $row['Email'];
        echo $row['IP'];
    }
    else {
        echo 'success';
    }
}else{
    die ('unable to run: ' .mysql_error());
}

For first you should use mysql_fetch_assoc() for variables which are in query

Check if column names are written properly and you should do this like that:

$query = "SELECT * FROM tbl_1 WHERE Email='$Email' AND IP='$ip'";
$Result = mysql_query($query) or die ('unable to run: ' .mysql_error());

if(mysql_num_rows($Result)) 
{
    echo 'You have already entered this competition!';
}
else {
    echo 'success';
}

because propably you have more than 1 record with this email and ip in mysql it saying success

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top