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 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts

    Unhappy Duplicate content problem!

    I have created an input field on a website for people to subscribe by their email address. The email address is stored in a database. I am using PHPMyAdmin.

    The email address is successfully working, but I want to prevent duplicate email address to be stored, however, I am having an error. Here are my codes:

    HTML codes:

    <form action="index.php" method="post">
    <input type="text" size="25" placeholder="Your email address..." name="enter"/>
    <input class="submit" type="submit" value="Subscribe" name="subscribe"/>
    <br/>

    PHP with Query codes:

    <?php
    if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
    $ee = htmlentities($_POST['enter']);
    if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$ee) || empty($ee)){
    echo '<p class="fail">Failed...Try again!</p>';
    } else {
    @mysql_connect ('localhost', 'root', '') or die ('A problem has occurred, refresh the page and try again!');
    @mysql_select_db ('links') or die ('A problem has occurred, refresh the page and try again!');
    $duplicate = "SELECT * FROM `email` WHERE `emailaadress` = '{$ee}'";
    $query = "INSERT INTO email (id, emailaddress) VALUES('NULL', '.$ee')";
    $result = mysql_query($duplicate);
    if ( mysql_num_rows ( $result ) > 1) {
    /* Username already exists */
    echo 'Username already exists';
    } else {
    mysql_query($query);
    echo '<p class="success">Successfully subscribed!</p>';
    }
    }
    }
    ?>
    </form>

    Error I am having:

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\softwareportfolio\index.php on line 68

    Can someone help me? Thank

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Change
    PHP Code:
    $result mysql_query($duplicate); 
    to
    PHP Code:
    $result mysql_query($duplicate) or die(mysql_error()); 
    to check for any errors int he query.

    btw, you could set a primary/unique key to the email field, to keep them unique.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Well I ma getting another errors now... But I will apply what you said, can you tell me what you mean by applying a primary/unique key for the field in my table? You mean, I put a primary of it just like I did for ID?

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Well I ma getting another errors now...
    Tyy to fix it first.

    If you already have a primary key, then set a unique key to the email field. After that you may simply run the insert query and analyse the output of mysql_errno() to inform the user.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Ok, I made it unique the field for email address in my table, and it is working I see. Can I know how to tell the user that his email address is already registered if he tries to subscribe again? I mean how to do that to make him aware?

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Have you read my above post?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Yes, I hav tried to search on it but in vain. I am new in PHP that's why I asked how to do it

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    The easiest way is to first do a SELECT query to see if the email address is already in the DB table.

    Yes, you can and should make the email address a unique key, but then you will have to parse the error message you would get from your INSERT query to make sure that the duplicate key is the reason for the error.

    Much simpler to just make the SELECT test, first.
    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.

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

    Doing it that way halves the number of database calls to perform the test and is therefore much faster.

    It is just the same as where you want to update a record if it already exists or insert it if it doesn't where the most efficient way is to pick whichever of insert or update you expect to work more often than not and call the other if that one fails (rather than the far less efficient way of doing a select first to see whether the record already exists).

    For the greatest efficiency of your database accesses set up the appropriate keys and make use of error handling when database queries fail rather than making extra unnecessary calls that slow things down.
    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.

  • #10
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    I have tried to do it, but I think I did it bad, here the codes:

    PHP Code:
    <?php
    if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
    @
    mysql_connect ('localhost''root''') or die ('A problem has occurred, refresh the page and try again!');
    @
    mysql_select_db ('links') or die ('A problem has occurred, refresh the page and try again!');
    $ee htmlentities($_POST['enter']);
    if (!
    preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$ee) || empty($ee)){
    echo 
    '<p class="fail">Failed! You will be redirected in 3 seconds!</p>';
    header'refresh:3; url= index.php' );
    return 
    false;
    } else {
    $duplicate "SELECT * email WHERE emailaddress";
    if (
    $duplicate === $ee) {
    echo 
    "Your email is already registered!";
    } else {
    $query "INSERT INTO email (id, emailaddress) VALUES('NULL', '.$ee')";
    mysql_query($query); 
    echo 
    '<p class="success">Successfully subscribed!</p>';
    header'refresh:2; url= index.php' );
    }
    }
    }
    ?>

    *Note, my database name is 'links', the table is 'email' and the email field is 'emailaddress'

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Assuming that you have the unique key set up on the field the following should do what you want

    PHP Code:
    <?php
    if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
    @
    mysql_connect ('localhost''root''') or die ('A problem has occurred, refresh the page and try again!');
    @
    mysql_select_db ('links') 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); 
    if (
    mysql_errorno() == 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.

  • #12
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Code:
    $duplicate = "SELECT * email WHERE emailaddress";
    if ($duplicate === $ee) {
    echo "Your email is already registered!";
    Don't you need to _query the DB using that sql and fetch the data from the result set?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #13
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    @FELGALL, I am getting this error:

    Fatal error: Call to undefined function mysql_errorno() in C:\xampp\htdocs\softwareportfolio\index.php on line 58

  • #14
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    I have changed it to: (mysql_errno() == 1022), still not work... i am searching on this functionality since morning!

  • #15
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    I have tried to make some more corrections, still not work, here are codes:

    PHP Code:
    <?php
    if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
    @
    mysql_connect ('localhost''root''') or die ('A problem has occurred, refresh the page and try again!');
    @
    mysql_select_db ('links') or die ('A problem has occurred, refresh the page and try again!');
    $ee mysql_real_escape_string($_POST['enter']);
    $emailCheck mysql_query("SELECT emailaddress FROM email WHERE emailaddress = '".$ee."'");
    if (!
    preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$ee) || empty($ee)){
    echo 
    '<p class="fail">Failed! You will be redirected in 3 seconds!</p>';
    header'refresh:3; url= index.php' );
    return 
    false;

    elseif (
    mysql_num_rows($emailCheck) > 0) {
    echo(
    "EMAIL Already exists");
    }
    else {
    $query "INSERT INTO email (id, emailaddress) VALUES('NULL', '.$ee')";
    mysql_query($query); 
    echo 
    '<p class="success">Successfully subscribed!</p>';
    header'refresh:2; url= index.php' );
    }
    }
    ?>


  •  
    Page 1 of 2 12 LastLast

    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
    •