Search code examples
phpmysqlmysql-num-rows

mysql_num_rows if count is greater than 1


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';
}

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/.