1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Duplicate entry '1-22' for key 1

Discussion in 'PHP' started by 123GoToAndPlay, Apr 22, 2009.

  1. #1
    Anyone knows where to look for?

    I just truncate the mysql table i am using so no id's are set. Also the ID is set to BIGINT(20), still i get Duplicate entry '1-22' for key 1 error

    Any suggestions?
     
    123GoToAndPlay, Apr 22, 2009 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Can you please provide table structure and your statement (probably INSERT or UPDATE) which causes the error.

    Regards
     
    koko5, Apr 22, 2009 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi koko5,

    here's my insert query

    
    $sql = "INSERT INTO `wp_postsTest` ( `ID` , `post_author` , `post_date` , `post_date_gmt` , `post_content` , `post_title` , `post_category` , `post_excerpt` , `post_status` , `comment_status` , `ping_status` , `post_password` , `post_name` , `to_ping` , `pinged` , `post_modified` , `post_modified_gmt` , `post_content_filtered` , `post_parent` , `guid` , `menu_order` , `post_type` , `post_mime_type` , `comment_count` ) VALUES (NULL , '$post_author', '$post_date', '0000-00-00 00:00:00', '$post_content', '$post_title', '0', '', 'publish', 'open', 'open', '', '$post_name', '', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', '0', '', '0', 'post', '', '0')";
    
    PHP:
     
    123GoToAndPlay, Apr 22, 2009 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Hi, 123GoToAndPlay,

    Using which one query you did erase all data from table: TRUNCATE or DELETE?
    You can prevent errors using REPLACE instead INSERT in your query but it's not clear solution about this error.

    Regards
    Edit: I mean truncate is best for erasing all data. So run truncate if you previous run delete and let's check is there any error during Insert.
     
    koko5, Apr 22, 2009 IP
  5. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hmm i use phpmyadmin to empty/truncate all data in wp_postsTest.

    So your suggestion is to query
    DELETE FROM wp_postsTest

    I thought both would be the same as the table will be empty
     
    123GoToAndPlay, Apr 22, 2009 IP
  6. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hmm, i DELETE FROM

    but now it's stuck and i get the error

    Duplicate entry '65-22' for key 1
     
    123GoToAndPlay, Apr 22, 2009 IP
  7. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #7
    No, I suggest you use truncate via console or phpmyadmin query window because truncate resets auto_increment fileds but delete doesn't.

    You didn't provide table structure but did you run one select query to check is there any data left in table after this manual deletion which can cause such error?

    Just run truncate and try your query without modifications and let me know the result.
    Regards
     
    koko5, Apr 22, 2009 IP
  8. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    tx, for the effort so far

    i truncate the table and i used my original insert query.

    again i get the
    Duplicate entry '1-22' for key 1
    error

    i do have this query in a php for loop.

    What is the way to check if a table is really "clean/empty"?? Or should i just export the table structure and delete the table and create a new one?
     
    123GoToAndPlay, Apr 22, 2009 IP
  9. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #9
    You can check is table empty by query like this:

    
    SELECT EXISTS(SELECT * FROM wp_postsTest);
    
    Code (markup):
    or
    
    SELECT COUNT(*) FROM wp_postsTest;
    
    Code (markup):
    It can be error either during php loop or MyISAM broken table.
    You can check by using REPLACE instead INSERT in your query (other code is untouched; only replace words INSERT by REPLACE) and check how much records were Inserted during loop (if lower than expected than loop generated same values that are overwritten OR MyISAM table is broken and it's better to drop&create it); and are there any errors.
    Hope it helps.Regards

    Edit: If when using replace duplicate error occurs it is broken table I guess.
     
    koko5, Apr 22, 2009 IP
  10. SiteTalkZone

    SiteTalkZone Peon

    Messages:
    243
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Is autoincrement for the ID feild in your table turned on?
     
    SiteTalkZone, Apr 22, 2009 IP
  11. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    @siteTalkZone, yes the ID field is set to auto increment

    @koko5, done all your suggestions without any succes :(.

    I even drop and create the table

    Now i am trying to use simple contents for my $vars like "test" and "1234" and see what happens

    AHA, this looks to run good so it has to do with my $var contents.

    It's a extern product feed which i parse and i use addslashes to prevent quotes. But the auto increment column is NULL so duplicate entry shouldn't occur, right??

    Which function should i use to prevent the duplicat entry error
     
    123GoToAndPlay, Apr 23, 2009 IP
  12. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #12
    It's better using mysql_real_escape_string instead addslashes.

    You can omit ID and NULL :
    
    $sql = "INSERT INTO `wp_postsTest` (`post_author` , `post_date` , `post_date_gmt` , `post_content` , `post_title` , `post_category` , `post_excerpt` , `post_status` , `comment_status` , `ping_status` , `post_password` , `post_name` , `to_ping` , `pinged` , `post_modified` , `post_modified_gmt` , `post_content_filtered` , `post_parent` , `guid` , `menu_order` , `post_type` , `post_mime_type` , `comment_count` ) VALUES ('$post_author', '$post_date', '0000-00-00 00:00:00', '$post_content', '$post_title', '0', '', 'publish', 'open', 'open', '', '$post_name', '', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', '0', '', '0', 'post', '', '0')";
    
    PHP:
    You can echo $sql instead of executing queries in the loop and then copy&paste each query into phpmyadmin and check why and where error occurs.
     
    koko5, Apr 23, 2009 IP