Welcome to WebmasterWorld Guest from 23.20.223.88

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Eliminating Repeating Data Entry

     
12:49 am on Dec 8, 2008 (gmt 0)

5+ Year Member



Hi there,
on my website I plan to implement a system by where the users email is their login on the premis that all emails are unique.

However, when checking to see if the email they entered already exists I get a logical error by where the existing email variable is never equal to 0.

0 is what the variable would be at if there were no existing records.

Here's my code:

$existing_email = 0;
$result = mysql_query("SELECT COUNT(*) FROM user WHERE user_email = '$email_1'");
while($row = mysql_fetch_array($result))
{
$existing_email ++;
}

Then below, during my validation, I have the following:

elseif($existing_email != 0)
{
header('Location: '. ERROR);
}

Any help would be great because I'm stumped!

Regards,
Tom.

1:10 am on Dec 8, 2008 (gmt 0)

5+ Year Member



Hint: How many rows does <
SELECT COUNT(*) FROM user WHERE user_email = '$email_1';
> return?
1:52 am on Dec 8, 2008 (gmt 0)

5+ Year Member



It always returns 1. I've no idea why.

Edit:
I've used a different approach. Here's my result for people who are curious in future:


$result = mysql_query("SELECT user_email FROM user WHERE user_email = '$email_1'");
while($row = mysql_fetch_array($result))
{
$existing_email = $row['user_email'];
}

and then the error checking...


elseif($email_1 == $existing_email)
{
header('Location: '. ERROR);
}

[edited by: Tom_Cash at 2:01 am (utc) on Dec. 8, 2008]

2:56 am on Dec 8, 2008 (gmt 0)

5+ Year Member



SELECT COUNT(*) FROM user WHERE user_email = '$email_1'
returns exactly one row of one item: the number of rows in your database for which
user_email = '$email_1'
is true.

Thus, if the e-mail address is not in your database, it returns the single row

[ 0 ]
, and if the e-mail address is in your database, it returns the row
[ 1 ]
. Your while loop, meanwhile, was counting the number of rows returned--which is always one!

For the COUNT(*) solution to work, you have to actually use the count returned.

while($row = mysql_fetch_array($result))
{
print_r($row); // add to see what $row looks like
$existing_email ++;
}
4:30 am on Dec 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




For the COUNT(*) solution to work, you have to actually use the count returned.

Exactly, pinterface is Correct. This Simple thing should do the trick


$result = mysql_query("SELECT COUNT(*) as existing_email_count FROM user WHERE user_email = '$email_1'");
$row = mysql_fetch_array($result);
$existing_email=$row['existing_email_count'];

Simple ;)

[edited by: Anyango at 4:31 am (utc) on Dec. 8, 2008]

8:06 am on Dec 8, 2008 (gmt 0)

5+ Year Member



Thanks for the futher replies peeps!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month