SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Duplicate entries with one command?

    I have a script where I want a record for each user, but only one record for each user, but somehow, randomly, it inserts duplicate records for some users?

    Here is what i got.
    PHP Code:
    $sql mysql_query("SELECT * FROM ".$prefix."_userstrings WHERE fk_player_id=$playerid");
    $row mysql_fetch_assoc($sql);

    if(!
    $row){
        
    $sql mysql_query("INSERT ".$prefix."_userstrings (fk_player_id,timestamp,active,thestring) VALUES ($playerid,$timestamp,1,'$dbstring')");
        
    mysql_close($con); 
        exit;
    } else {
        
    $sql mysql_query("UPDATE ".$prefix."_userstrings SET timestamp = $timestamp, active=1, thestring='$dbstring' WHERE fk_player_id=$playerid");
        
    mysql_close($con);
        exit;

    I have tried with the exit and mysql_close() but still the same. What can i do to make sure that I don't get duplicate inserts?

    Thanks in advance :-)

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Put a unique index on fk_player_id and then use the INSERT ... ON DUPLICATE KEY UPDATE syntax.

    And an advice: since the mysql_ database extension is becoming deprecated, you might want to take a look at mysqli or pdo


Bookmarks

Posting Permissions

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