Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9
This is the forum to discuss the Wrox book PHP and MySQL: Create-Modify-Reuse by Timothy Boronczyk, Martin E. Psinas; ISBN: 9780470192429
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 4th, 2011, 06:56 PM
Authorized User
Points: 59, Level: 1
Points: 59, Level: 1 Points: 59, Level: 1 Points: 59, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 12
Thanks: 0
Thanked 2 Times in 2 Posts
Default Ch1 multiple username with same email address

I am able to register more than one username with the same email address. Should this be happening? So, I'm trying to program an email address checker to see if it is already being used. I'm looking at register.php and User.php and trying to write something like what is already written to check if the username exists. I'm still trying to get my head around OOP and wonder if someone could give me a pointer.
Reply With Quote
  #2 (permalink)  
Old June 5th, 2011, 09:03 PM
Authorized User
Points: 374, Level: 6
Points: 374, Level: 6 Points: 374, Level: 6 Points: 374, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2009
Location: San Jose, Californina
Posts: 77
Thanks: 4
Thanked 6 Times in 6 Posts
Default

Hi Albion,

The code does not check for duplicate email addresses.
There are reasons for allowing duplicate emails.


There may be cases where people use the same
email such as supportstaff@somecompany.com

You would have to be careful about what you
say to someone who is attempting registration
at your site. If you said that email is already
in use at your site, then you have given away an
existing members email address.

You have probably already thought these things
through.

If you do decide you want to force uniqueness, here
are some ways you can do it. You can do it on any number
of fields. Let's use your example of email.


METHOD 1

You can use the UNIQUE constraint in the database
layout.

For example,
EMAIL_ADDR VARCHAR(100) NOT NULL UNIQUE,

This way a duplicate email address cannot be added. You
would have to adjust your code. Currently, if everything
is working right, and you enter a duplicate email address,
the query will succeed. With the UNIQUE constraint it
will now fail. You would have to check for that failure
and deal with that. mysql_query() will return false if it is a
duplicate. After your call to mysql_query you would need
to call mysql_error() to verify
the failure was indeed due to duplicate entry. mysql_error will
return a message like "Duplicate entry 'someone@acompany.com'
for key 2". You would need to do this in all the places where
you do a mysql_query to insert or update into the USER table.



METHOD 2

This one should be easier for the code in this book.

In User.php. there is a function named getByUsername. You can see this
on page 7 line2. You could add a new function called getByEmailAddress($email).
You could do this by copying and pasting this function and making a
few modifications.


Then back in register.php, you could have a check similar
to the user name check. This check is on page 14 about the
last 5 lines on the page. You could add another else if block
to do the check for duplicate email

PHP Code:
$user User::getByEmailAddress($_POST['email']);
if ( 
$user->userId)
   ...
please use another email address... 
I hope this helps.
Reply With Quote
  #3 (permalink)  
Old June 6th, 2011, 11:00 AM
Authorized User
Points: 59, Level: 1
Points: 59, Level: 1 Points: 59, Level: 1 Points: 59, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 12
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Thanks Kenj. Great advice. I'm chuffed (being new to PHP and programing) that your second method coincides with what I am trying to do. I'm certainly learning from the book. However, I thought through the problem once again, and what really confused me was that when I added a second user with a different username - but the same email address @company.com - then logging-in after verification it returned the main.php (change password) with the username and email address of the 1st user! Is there a logic to this that I'm missing.

Thanks
Reply With Quote
  #4 (permalink)  
Old June 6th, 2011, 06:01 PM
Authorized User
Points: 59, Level: 1
Points: 59, Level: 1 Points: 59, Level: 1 Points: 59, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 12
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Hi Kenj,

Save your breathe, I've figured it out.

Thanks
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Valid Email Address...... muskaanbajaj VB.NET 1 November 22nd, 2005 11:38 AM
How to validate email address crmpicco Javascript How-To 2 February 16th, 2005 10:56 AM
Validate Email address richardtr BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 6 November 23rd, 2004 01:45 PM



All times are GMT -4. The time now is 10:48 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.