0

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';
}
Arylis
  • 5
  • 1
  • 4
  • Are you sure your IP is changing? Have you checked the queries to make sure they are different? Are you sure that the data you think is in the database is actually there? – John Conde Apr 15 '14 at 18:38
  • Yes, I have checked my Table in the database.. It has stored the email, IP, timestamp and name, With this query I wish to scan the table to see if the current email/ip already exists in a row. I have 2 rows, with both the same name/email but different IP's, yet it's still returning successful :/ – Arylis Apr 15 '14 at 18:43
  • echo your query,then you can see the query which is executing – ɹɐqʞɐ zoɹǝɟ Apr 15 '14 at 18:54
  • SELECT * FROM tbl_1 WHERE email='xxxx@gmail.com' AND ip='xx.xxx.xx.80' – Arylis Apr 15 '14 at 18:57
  • @Arylis OK, I tested your code and there's nothing wrong with it. What is your IP column type, VARCHAR? other? – Funk Forty Niner Apr 15 '14 at 19:04
  • http://prntscr.com/3aau2y ...This is what im running. It checks the email, but not the IP, i've also tried with name, but it refuses to check the name too. It's only scanning for the email – Arylis Apr 15 '14 at 19:09
  • Yes, its var char,.. `code: email varchar(64) CHARACTER SET utf8 NOT NULL DEFAULT '', ip varchar(64) NOT NULL,` – Arylis Apr 15 '14 at 19:11
  • `$Email` and `$ip` are coming from a form? @Arylis – Funk Forty Niner Apr 15 '14 at 19:16
  • `code $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 – Arylis Apr 15 '14 at 19:19
  • Are you 100% sure you're using `$IP = $_SERVER["REMOTE_ADDR"];` and not `$ip = $_SERVER["REMOTE_ADDR"];`? `$IP` and `$ip` are NOT the same thing; it's case-sensitive. @Arylis which is most likely the problem here. – Funk Forty Niner Apr 15 '14 at 19:25
  • Oh I see. Well the variable I have set is actually. `$qq = $_SERVER["REMOTE_ADDR"];` ... Then using $qq to insert the variable into the row. `(IP) values ('$qq',)` and as for the SELECT query, `AND IP='$qq'";` – Arylis Apr 15 '14 at 19:36
  • Yes you can use that if you want, however I posted [**an answer**](http://stackoverflow.com/a/23092830/) with an explanation so we can close this now. @Arylis – Funk Forty Niner Apr 15 '14 at 19:38
  • Wait, is that want you REALLY are/were using for code originally, or have you now changed it to `qq`? @Arylis – Funk Forty Niner Apr 15 '14 at 19:39
  • It's what I changed too, since I thought it would be less confusing using a different variable all together to identify the IP. However, I've re-changed my code just now to your reply. Thank you so much for your time sir. – Arylis Apr 15 '14 at 19:43
  • You're quite welcome, was glad to have helped and resolved this for you. @Arylis – Funk Forty Niner Apr 15 '14 at 19:43
  • That's what I like to hear ;-) @Arylis Cheers :) – Funk Forty Niner Apr 15 '14 at 19:52

3 Answers3

2

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

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

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());
}
meda
  • 45,103
  • 14
  • 92
  • 122
-1

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

darkanzali
  • 85
  • 1
  • 1
  • 10
  • Well, the script checks to see if the email/ip exists, if it doesnt then it will insert your details. But it keeps inserting my details each time I change my IP, so i have multiple rows with the same name/email just different IP's. However, if I DONT change my IP it declines me – Arylis Apr 15 '14 at 18:51
  • Since IP's can change, you shouldn't be checking for that, just an email should/will suffice. @Arylis You will first need to query that row with a specific IP in order for it to work. Try `if($count > 0)` – Funk Forty Niner Apr 15 '14 at 18:53
  • I understand, but I'd like to lower the chances of people trying more than once on the time they enter – Arylis Apr 15 '14 at 18:58
  • You can use `OR` instead of `AND` @Arylis 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. – Funk Forty Niner Apr 15 '14 at 19:05