Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-21-2012, 08:38 PM   PM User | #16
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by felgall View Post
If you have it set up as a unique key then the easiest way is to simply try to insert whatever email address they enter. If the insert works then it isn't a duplicate - if the insert fails then you can produce appropriate error handling to advise them of the duplication in the die clause.
While I agree this is the *better* way to go, it is not the simplest way, especially for beginners.

A beginner is likely to have other problems in the INSERT query and won't be able to distinguish between those problems and the duplicate key problem.

If you think you can lead this person by the hand through parsing the error message and figuring out whether the error is a duplicate key or not, then go to it.

I would personally rather have the newbie do it in the two steps, just to isolate the problems better.

I doubt seriously that a newbie will be constructing a website where the difference between one or two queries per page is going to matter. More than likely, the page will only be hit a few times per day, at most, so who cares about a miniscule peformance difference?

But, again, if you are ready to lead this newbie through the parsing of the error message(s), go for it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-21-2012, 08:41 PM   PM User | #17
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by angelali View Post
I have tried to make some more corrections, still not work,
And what does "still not work" mean???

HOW does it "not work"?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-21-2012, 08:42 PM   PM User | #18
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Possibly the following where it actually passes the database resource to the appropriate calls might work (sorry about the typo in errno):

PHP Code:
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
$db = @mysql_connect ('localhost''root''') or die ('A problem has occurred, refresh the page and try again!');
@
mysql_select_db ('links',$db) or die ('A problem has occurred, refresh the page and try again!');
$ee $_POST['enter'];
if(!
filter_var($eeFILTER_VALIDATE_EMAIL)){
echo 
'<p class="fail">Failed! You will be redirected in 3 seconds!</p>';
header'refresh:3; url= index.php' );
return 
false;
} else {
$query "INSERT INTO email (id, emailaddress) VALUES('NULL', '.$ee')";
mysql_query($query,$db); 
if (
mysql_errno($db) == 1022) {
echo 
"Your email is already registered!";
} else {
echo 
'<p class="success">Successfully subscribed!</p>';
header'refresh:2; url= index.php' );
}
}
}
?>
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 02-22-2012, 03:58 AM   PM User | #19
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
I mean the email address is still registering even it is already registered. well it not being duplicate in the database as the field is UNIQUE, but on the site, the message to say the email address is already registered for subscription is not displaying.

I can leave this problem, but I would like the message that the email address is already existed to be displayed. From this feature, I can also know how to prevent duplicate username in another website. i mean i will also learn from it. that's why i was asking how to prevent duplicate content inserting into the database.
angelali is offline   Reply With Quote
Old 02-22-2012, 04:09 AM   PM User | #20
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
Guys, I have successfully tried to debug it myself and it is working now...YEAHHHHHHHHHHHHH

I changed the code you suggested me, instead of (mysql_errno($db) == 1022), I have used my brain, and changed it to (mysql_errno($db)> 0), and tested it, and it seems it is working now! .

This feature of checking duplicate content will serve me a lot. For example, if I have to create a login form, I can make the username field UNIQUE key, then use this feature to prevent duplicate content... I can learn a lot from this!

Last edited by angelali; 02-22-2012 at 04:15 AM..
angelali is offline   Reply With Quote
Reply

Bookmarks

Tags
duplicate, email address, mysql, store

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:18 PM.


Advertisement
Log in to turn off these ads.