+ Reply to Thread
Results 1 to 10 of 10

Thread: Error: Duplicate entry '' for key 4

  1. #1
    elnasiru96 is offline x10Hosting Member elnasiru96 is an unknown quantity at this point
    Join Date
    Jun 2010
    Posts
    11

    Error: Duplicate entry '' for key 4

    pls im having an "(Error: Duplicate entry '' for key 4)" on testing registration of new members on my website through thier email addresses. any helps on how to avoid this annoying sting
    ? thanx
    Last edited by elnasiru96; 08-04-2010 at 04:34 PM.

  2. #2
    AngusThermopyle is offline x10Hosting Member AngusThermopyle is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    84

    Re: Error: Duplicate entry '' for key 4

    Test for the presence of the value before you try to insert.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Error: Duplicate entry '' for key 4

    Show us the code (in particular, a minimal test case consisting of PHP and SQL) and DB structure. Otherwise, we can only guess as to cause.

    So you don't increase load on the MySQL server, rather than issuing additional queries you can suppress or disable error messages and test the result of running the insertion query. If the query fails due to a duplicate key, inform the user that that particular e-mail address (or whatever duplicate datum) is already registered, perhaps including a link to your password reset system.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    elnasiru96 is offline x10Hosting Member elnasiru96 is an unknown quantity at this point
    Join Date
    Jun 2010
    Posts
    11

    Re: Error: Duplicate entry '' for key 4

    here's a brief on the script that's generating the error.

    ##User isn't registering, check verify code and change activation code to null, status to activated on success

    $queryString = $_SERVER['QUERY_STRING'];

    $query = "SELECT * FROM users";

    $result = mysqli_query($link,$query) or die(mysqli_error($link));

    while($row = mysqli_fetch_array($result)){

    if ($queryString == $row["activationkey"]){

    echo "Congratulations!" . $row["username"] . " is now the proud new owner of a carrygo.com account.";

    $sql="UPDATE users SET activationkey = '', status='activated' WHERE (id = $row[id])";

    if (!mysqli_query($link,$sql))

  5. #5
    chankarwing's Avatar
    chankarwing is offline x10Hosting Member chankarwing is an unknown quantity at this point
    Join Date
    Jun 2008
    Posts
    15

    Re: Error: Duplicate entry '' for key 4

    since each users will be unique. you should use auto increment.
    Kind Regards,
    Gavin



    Why don't you give me a visit?

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Error: Duplicate entry '' for key 4

    Please use [php], [html] or [code] tags (as appropriate) to separate and formate code.

    Quote Originally Posted by elnasiru96 View Post
    here's a brief on the script that's generating the error.
    What about the table structure? Is column activationkey unique? Is it in the primary index? Is it allowed to be Null?

    Quote Originally Posted by elnasiru96 View Post
    PHP Code:
    ##User isn't registering, check verify code and change activation code to null, status to activated on success

    $queryString $_SERVER['QUERY_STRING'];
    $query "SELECT * FROM users"
    Don't use SELECT *; select only the columns you need. The statement also needs a WHERE clause. Otherwise, you're fetching every entry in the `users` table, which is a terrible waste of server resources.

    PHP Code:
    $userRegisteredQuery mysqli_prepare('SELECT COUNT(*) FROM users WHERE activationkey = ?');
    $userRegisteredQuery->bind_param('s'$_SERVER['QUERY_STRING']); 
    Quote Originally Posted by elnasiru96 View Post
    PHP Code:
    $result mysqli_query($link,$query) or die(mysqli_error($link)); 
    Don't use die when outputting HTML. Don't output MySQL error messages to non-administrators; it reveals too much information.



    The code can be simplified to use a single query. I also recommend switching to PDO, which is simpler to use and more powerful than mysqli.

    PHP Code:
    <?php
    $activate 
    $db->prepare("UPDATE users 
            SET activationkey = NULL, status='activated' 
            WHERE activationkey = ?"
    );
    if (
    $activate->execute(array($_SERVER['QUERY_STRING']))) {
        if (
    $activate->rowCount()) {
            echo 
    "Congratulations! You are now the proud new owner of a carrygo.com account.";
            
    // log in user?
            
    ...
        } else { 
    // no affected rows. 
            /* Most likely an invalid activation code, or account is already activated.
               Include link to password reset & possibly a way to check if they're registered.
               The password reset mechanism may also function with the latter purpose.
             */
            
    ?><p>I couldn't find that activation code. You might have already activated 
            your account. If you've forgotten your password [...]
            </p><?php
        
    }
    } else { 
    // execution failed
      
    ?><p>There was an internal error trying to activate your account. It's been logged, 
      and we'll log into it. Please give us some time to fix it and try again later.</p><?php
      $error 
    $activate->errorInfo();
      
    error_log("Activation for '$_SERVER[QUERY_STRING]' failed: $error[2] ($error[0]/$error[1]).")
      ...
    }
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  7. #7
    elnasiru96 is offline x10Hosting Member elnasiru96 is an unknown quantity at this point
    Join Date
    Jun 2010
    Posts
    11

    Re: Error: Duplicate entry '' for key 4

    here's the table structure

    CREATE TABLE IF NOT EXISTS `users` (
    `id` int(11) NOT NULL auto_increment,
    `status` varchar(20) NOT NULL,
    `username` varchar(20) NOT NULL,
    `password` varchar(20) NOT NULL,
    `email` varchar(20) NOT NULL,
    `activationkey` varchar(100) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `username` (`username`),
    UNIQUE KEY `email` (`email`),
    UNIQUE KEY `activationkey` (`activationkey`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

  8. #8
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Error: Duplicate entry '' for key 4

    Code:
    UNIQUE KEY `activationkey` (`activationkey`)
    There is your problem. KEY, ok. UNIQUE, not ok.

    When you activate the user, you set the activationkey to an empty string.
    The second time you activate a user, the field is not unique.

    1. Change the KEY so it is not UNIQUE
    2. Don't null out the KEY
    3. Put something else 'unique' in as a replacement (convert the ID to a string maybe).
    Nothing is always absolutely so.

  9. #9
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Error: Duplicate entry '' for key 4

    I'm going to have to disagree with Descalzo about the activation key being unique in the current design. Notionally, the activation key is unique to an account, since it's sufficient to identify an unactivated account; it's a candidate key. If the activation key and some other piece of identifying information (e.g. e-mail, username) were required, the activation key wouldn't be a candidate key. Moreover, the DB should enforce uniqueness. Declaring the column in a unique index is thus appropriate.

    Removing the UNIQUE attribute from the activation key index isn't necessary to resolve the issue. Storing some other unique data (such as Descalzo suggests) would work, but someone could potentially re-activate their account using the new data (not that this would be problematic, though it is odd). Allowing the column to store NULL values would also work, since NULL doesn't equal itself using the standard comparison operators in MySQL. Nulling the activation key for activated accounts also lets you change the column type to a constant width CHAR type (since it's a fair assumption that activation keys are all of a length), which offers some performance advantages.

    A third (and better) approach is to separate out the activation data. One reason it's better is that storing the activation key in the user table is wasteful.

    One particular approach is to keep a table of pending user accounts. When an account is activated, move the entry from the `pending` table to the `users` table.

    Code:
    INSERT INTO users SELECT username, password, email, status FROM pending WHERE activationkey = ?;
    -- if the above succeeds
    DELETE FROM pending WHERE activationkey = ?;
    Alternatively, the pending table could store the activation key and the ID of the user to be activated, with the rest of the user data in the `users` table. One downside to this is that corruption of the pending table will essentially authorize pending users.

    Depending on how the authentication and authorization system is designed, each option will have other benefits and detriments. With the first option, the default behavior is that unactivated users aren't given access; with the latter, unactivated users can log in without special consideration.
    Last edited by misson; 08-05-2010 at 08:27 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  10. #10
    sith is offline x10Hosting Member sith is an unknown quantity at this point
    Join Date
    Oct 2010
    Posts
    1

    Re: Error: Duplicate entry '' for key 4

    I agree with descalzo you should remove UNIQUE KEY `activationkey` (`activationkey`)

    Quote Originally Posted by elnasiru96 View Post
    here's the table structure
    CREATE TABLE IF NOT EXISTS `users` (
    `id` int(11) NOT NULL auto_increment,
    `status` varchar(20) NOT NULL,
    `username` varchar(20) NOT NULL,
    `password` varchar(20) NOT NULL,
    `email` varchar(20) NOT NULL,
    `activationkey` varchar(100) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `username` (`username`),
    UNIQUE KEY `email` (`email`),
    UNIQUE KEY `activationkey` (`activationkey`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

    Just create new table, so it will be like this :

    CREATE TABLE IF NOT EXISTS `users` (
    `id` int(11) NOT NULL auto_increment,
    `status` varchar(20) NOT NULL,
    `username` varchar(20) NOT NULL,
    `password` varchar(20) NOT NULL,
    `email` varchar(20) NOT NULL,
    `activationkey` varchar(100) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `username` (`username`),
    UNIQUE KEY `email` (`email`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    Have a nice day.
    Last edited by sith; 10-21-2010 at 12:11 AM.

+ Reply to Thread

Similar Threads

  1. Duplicate entry '0' for key 1
    By akselamazigh in forum Programming Help
    Replies: 2
    Last Post: 12-17-2009, 02:49 PM
  2. Error MX Entry
    By spiria in forum Free Hosting
    Replies: 0
    Last Post: 04-26-2009, 03:29 PM
  3. DNS Entry error
    By prakhar in forum Free Hosting
    Replies: 1
    Last Post: 08-24-2008, 10:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers