homepage Welcome to WebmasterWorld Guest from 54.205.241.107
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Eliminating Repeating Data Entry
Tom_Cash

5+ Year Member



 
Msg#: 3802205 posted 12:49 am on Dec 8, 2008 (gmt 0)

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.

 

pinterface

5+ Year Member



 
Msg#: 3802205 posted 1:10 am on Dec 8, 2008 (gmt 0)

Hint: How many rows does <
SELECT COUNT(*) FROM user WHERE user_email = '$email_1';> return?
Tom_Cash

5+ Year Member



 
Msg#: 3802205 posted 1:52 am on Dec 8, 2008 (gmt 0)

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]

pinterface

5+ Year Member



 
Msg#: 3802205 posted 2:56 am on Dec 8, 2008 (gmt 0)

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

Anyango

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3802205 posted 4:30 am on Dec 8, 2008 (gmt 0)


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]

Tom_Cash

5+ Year Member



 
Msg#: 3802205 posted 8:06 am on Dec 8, 2008 (gmt 0)

Thanks for the futher replies peeps!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved