Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,506
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    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.

  2. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,506
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    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.

  3. #18
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  4. #19
    Regular Coder
    Join Date
    Sep 2011
    Posts
    348
    Thanks
    39
    Thanked 0 Times in 0 Posts
    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.

  5. #20
    Regular Coder
    Join Date
    Sep 2011
    Posts
    348
    Thanks
    39
    Thanked 0 Times in 0 Posts
    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.


 
Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •