SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to deal with duplicate entry

    Hi,

    My website works fine for a long time. But today, I got database duplicate entry entry error When trying to add news to the home page.

    This is the entire error message

    HTML Code:
    <h1>A Database Error Occurred</h1>
    <p>Error Number: 1062</p>
    <p>Duplicate entry '111' for key 1</p>
    <p>INSERT INTO `news` (`news_title`, `news_detail`, `news_source`, `published_date`, `news_type`, `region`, `link`, `country`) VALUES ('sdsd', 'dfdf', 'saasds', 1273510800, '1', '2', 'sfdf', 'dfd')</p
    This is my table and some data

    Code:
    CREATE TABLE IF NOT EXISTS `news` (
      `id` int(11) NOT NULL auto_increment,
      `news_title` varchar(555) collate utf8_unicode_ci NOT NULL,
      `news_detail` mediumtext collate utf8_unicode_ci NOT NULL,
      `news_source` varchar(555) collate utf8_unicode_ci NOT NULL,
      `published_date` int(11) NOT NULL,
      `region` tinyint(4) NOT NULL,
      `news_type` int(111) NOT NULL,
      `link` varchar(333) collate utf8_unicode_ci NOT NULL,
      `country` varchar(333) collate utf8_unicode_ci NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=111 ;
    Code:
    INSERT INTO `news` (`id`, `news_title`, `news_detail`, `news_source`, `published_date`, `region`, `news_type`, `link`, `country`) VALUES
    (53, 'New Zealand''s Real Estate Market Expected To Slow In 2010', 'After an auspicious start in 2009, property sales have been down of late in New Zealand, and analysts expect the slowdown to continue in 2010. According to one analyst, though, this slowdown should not inspire panic and merely is leading towards a more balanced marketplace. See the following article from Property Wire for more on this. \r\n\r\nThe increase in residential property prices in New Zealand is slowing and sales declining as home loan interest rates rise, prompting worries about at dip in 2010. Nationally house prices rose 0.2&#37; in November, down from the 1.3% gained in October but they are still surging ahead in Auckland where demand for real estate remains high, the latest figures from the Real Estate Institute of New Zealand show.\r\n\r\nCompared to a year ago, the housing price index has increased by 6.6% and prices are now 2.9% below the peak of November 2007.\r\n\r\nBut property sales fell for a second month in November from 6,091 in October and 6,464 in September. While the median time to sell a house rose to 33 days from 31 in October, and fell from 44 days in November last year, the institute said.\r\n\r\nBut it is a different picture in Auckland where prices have soared nearly 12% over the past year compared with 4.7% in Wellington and 5.7% in Christchurch.\r\n\r\nAnalysts expect the slower pace to continue in 2010. Reserve Bank Governor Alan Bollard said last week that house price inflation would moderate in 2010, reducing the need for him to raise the benchmark interest rate before the middle of the year.  But lenders have increased home loan interest rates for fixed terms of one year or longer as global funding costs have increased, and this could curb demand for property.\r\n\r\n‘No longer do borrowers get the benefit of certainty and low rates that had been a feature of previous cycles. The prospect of higher interest rates and a still weak labor market will continue to be major headwinds facing the housing market,’ warned Khoon Goh senior economist at ANZ National Bank.\r\n\r\nUnemployment is also rising, up to a nine-year high of 6.5% in the third quarter of this year and predicted to reach 6.7% next year, and this could have an effect on the real estate market.\r\n\r\nThe level of sales is consistent with troughs in previous housing downturns, according to Goh. ‘It could hint at the start of an easing in housing market activity at a time when we are getting a supply response with a rise in the number of houses for sale,’ he said.\r\n\r\nIndeed sales volumes have fallen 13.6% over the past two months to the lowest level since March. ‘We would be surprised if this signaled a cyclical turn for the worst in housing. However the latest data, along with subdued weekly mortgage approvals, suggests the extraordinary bounce over the first half of 2009 is behind us, and the outlook for sales activity is far more balanced from here,’ said Bernard Doyle of Goldman Sachs.\r\n\r\n', 'nuwireinvestor.com', 1260982800, 1, 1, 'http://www.nuwireinvestor.com/articles/new-zealands-real-estate-market-expected-to-slow-in-2010-54249.aspx', 'New Zealand'),
    (57, 'Trắng hiện đại v&#224; trẻ trung', 'Trong ng&#244;i nh&#224; n&#224;y sắc trắng được sử dụng ở khắp mọi nơi - tường, cửa, tủ quần &#225;o, b&#224;n ăn, kệ... v&#224; l&#250;c n&#224;o cũng &#225;nh l&#234;n những m&#224;u kh&#225;c nhau theo mức độ phản chiếu &#225;nh s&#225;ng.\n\nNg&#244;i nh&#224; phố c&#243; bề ngang rộng chưa đến 4m, một ti&#234;u chuẩn phổ biến ở c&#225;c ng&#244;i nh&#224; phố kh&#225;c, v&#224; chiều d&#224;i chỉ khoảng 12m cũng kh&#244;ng l&#224;m b&#243; tay kiến tr&#250;c sư trong việc tạo n&#234;n kh&#244;ng gian tho&#225;ng m&#225;t cho ng&#244;i nh&#224;. Sự giản dị của m&#224;u trắng vẫn bật l&#234;n đường n&#233;t hiện đại, trẻ trung.\n\nTầng trệt với chiều d&#224;i khi&#234;m tốn được ph&#226;n th&#224;nh hai gian: một nơi để xe v&#224; khu vực ph&#242;ng ăn th&#244;ng với bếp ph&#226;n c&#225;ch bằng cầu thang ốp gỗ dẫn l&#234;n tầng lửng v&#224; hai lầu b&#234;n tr&#234;n. Trong khi c&#225;c tầng lầu 1 v&#224; 2 được bố tr&#237; c&#225;c ph&#242;ng ngủ v&#224; ph&#242;ng l&#224;m việc th&#236; trọn diện t&#237;ch của tầng lửng d&#224;nh cho ph&#242;ng kh&#225;ch, nơi đ&#225;p ứng được cả hai mục ti&#234;u vừa tho&#225;ng m&#225;t vừa k&#237;n đ&#225;o. Ngồi tại đ&#226;y c&#243; thể cảm thấy được sự th&#244;ng tho&#225;ng của kh&#244;ng gian v&#224; tận hưởng được &#225;nh s&#225;ng thi&#234;n nhi&#234;n chắt lọc từ giếng trời v&#224; cửa sổ rộng lớn b&#234;n h&#244;ng v&#224; mặt trước. Cửa sổ rộng nh&#236;n ra cầu thang v&#224; to&#224;n ng&#244;i nh&#224; lại được kh&#233;o l&#233;o che lại bởi ba chiếc cửa sổ nhỏ xinh ở mặt tiền để tạo sự ri&#234;ng tư cho kh&#244;ng gian nơi đ&#226;y.\n\nTuy chỉ với m&#224;u trắng giản dị được nối kết bằng c&#225;c m&#224;u nền đen hoặc n&#226;u gỗ v&#224; những điểm xanh xuy&#234;n suốt căn nh&#224;, kiến tr&#250;c sư đ&#227; xếp đặt v&#224; bố tr&#237; h&#224;i h&#242;a giữa m&#224;u sắc v&#224; kh&#244;ng gian nội thất tạo n&#234;n một cảm gi&#225;c dễ chịu. Sắc trắng được sử dụng ở khắp mọi nơi - tường, cửa, tủ quần &#225;o, b&#224;n ăn v&#224; kệ v&#224; l&#250;c n&#224;o cũng &#225;nh l&#234;n những m&#224;u kh&#225;c nhau theo mức độ phản chiếu &#225;nh s&#225;ng. Điểm xuyết tr&#234;n nền trắng n&#224;y l&#224; m&#224;u xanh của nệm ghế hay c&#225;c vật dụng trang tr&#237; nho nhỏ. Phải chăng c&#225;ch xử l&#253; kh&#244;n ngoan n&#224;y đ&#227; l&#224;m bật l&#234;n n&#233;t trẻ trung cho kh&#244;ng gian b&#234;n trong ng&#244;i nh&#224; v&#224; l&#224;m cho ng&#244;i nh&#224; trở n&#234;n nhiều m&#224;u sắc?\n\nBốn tấc giếng trời b&#234;n h&#244;ng nh&#224; kh&#244;ng những đ&#227; hữu dụng trong việc đem lại &#225;nh s&#225;ng v&#224; gi&#243; cho ph&#242;ng ăn, ph&#242;ng kh&#225;ch v&#224; ph&#242;ng l&#224;m việc m&#224; c&#242;n tạo được n&#234;n n&#233;t độc đ&#225;o ri&#234;ng cho ng&#244;i nh&#224;. Giếng trời b&#234;n h&#244;ng kết hợp với lựa chọn c&#225;c cửa ph&#242;ng kh&#225;ch, cửa bước ra ban c&#244;ng v&#224; cửa sổ hầu như được mở rộng, k&#233;o d&#224;i xuống tận s&#224;n v&#224; gắn k&#237;nh trong suốt đ&#227; mang lại n&#233;t tho&#225;ng mở v&#224; hiện đại cho từng kh&#244;ng gian c&#243; sự hiện diện của giếng trời như ph&#242;ng ăn dưới đất, ph&#242;ng kh&#225;ch ở tầng lửng v&#224; ph&#242;ng l&#224;m việc ở tầng lầu 1. C&#225;c đồ nội thất được thiết kế theo kiểu “tailor - made” (may đo) như kệ s&#225;ch, b&#224;n trang điểm, giường ngủ, gương trong ph&#242;ng tắm, b&#224;n ăn, b&#224;n uống tr&#224;… theo những đường thẳng tắp cũng g&#243;p phần tạo n&#234;n n&#233;t hiện đại v&#224; đơn giản.\n\nTrong một thế giới m&#224; h&#224;ng hiệu lu&#244;n chiếm lĩnh, việc tạo ra một phong c&#225;ch cho ng&#244;i nh&#224; của m&#236;nh theo sở th&#237;ch ri&#234;ng thật kh&#244;ng dễ d&#224;ng ch&#250;t n&#224;o. Th&#234;m v&#224;o đ&#243; l&#224;m thế n&#224;o để g&#243;i gọn c&#225;c &#253; tưởng của kiến tr&#250;c sự v&#224; nguyện vọng của chủ nh&#226;n trong một kh&#244;ng gian hạn hẹp cũng l&#224; một th&#225;ch thức. Vậy đ&#243;, s&#225;ng tạo, trẻ trung v&#224; hiện đại l&#224; th&#244;ng điệp m&#224; ng&#244;i nh&#224; b&#233; xinh n&#224;y muốn chuyển tải.\n', 'nhadep-magazine.com.vn', 1262019600, 2, 2, 'http://www.nhadep-magazine.com.vn/web/tintuc/default.aspx?cat_id=623&news_id=1171#content', 'Viet Nam'),

  2. #2
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know why and how to deal with it. Can mod help me delete this useless post..

  3. #3
    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)
    You could post the solution and make this a useful post for others that might have the same problem.

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when I constructed the news table, I didn't set primary key for the id column. Today a site maintainer manually delete a row in middle the news table, he then wants to add a news story using admin panel. So, duplicate entry error happen.

    I fix it by set the primary key for the id column. This mistake is silly.

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,252
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    Silly perhaps, but not so uncommon. A smart man learns from his mistakes, a wise man learns from other's mistakes. My bet is this thread will help many avoid making the same mistake. Thank you for posting the cause and the solution.

  6. #6
    SitePoint Member urbanhomes's Avatar
    Join Date
    Nov 2010
    Location
    Pune
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah! this type of error shows in my website..even i don't understand how to delete duplicate entries from my data base.. if you get solution please share with us

  7. #7
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by runrunforest View Post
    when I constructed the news table, I didn't set primary key for the id column. Today a site maintainer manually delete a row in middle the news table, he then wants to add a news story using admin panel. So, duplicate entry error happen.

    I fix it by set the primary key for the id column. This mistake is silly.
    but isn't this

    Code:
    PRIMARY KEY  (`id`)
    in your original code setting the primary key on id?

  8. #8
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Old thread but a new question was asked so here goes.

    It is unlikely for there to be two entries of 'news_title' and 'published_name' so that means they ought to be unique, with a unique index.

    Then add the ON DUPLICATE KEY UPDATE, to the INSERT query so that if a record is being added which would cause a duplicate entry, the original will be updated.

    bazz


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
  •