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

<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

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 ;
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\
\\r\
The 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% 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\
\\r\
Compared 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\
\\r\
But 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\
\\r\
But 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\
\\r\
Analysts 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\
\\r\
‘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\
\\r\
Unemployment 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\
\\r\
The 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\
\\r\
Indeed 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\
\\r\
', '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à trẻ trung', 'Trong ngôi nhà này sắc trắng được sử dụng ở khắp mọi nơi - tường, cửa, tủ quần áo, bàn ăn, kệ... và lúc nào cũng ánh lên những màu khác nhau theo mức độ phản chiếu ánh sáng.\
\
Ngôi nhà phố có bề ngang rộng chưa đến 4m, một tiêu chuẩn phổ biến ở các ngôi nhà phố khác, và chiều dài chỉ khoảng 12m cũng không làm bó tay kiến trúc sư trong việc tạo nên không gian thoáng mát cho ngôi nhà. Sự giản dị của màu trắng vẫn bật lên đường nét hiện đại, trẻ trung.\
\
Tầng trệt với chiều dài khiêm tốn được phân thành hai gian: một nơi để xe và khu vực phòng ăn thông với bếp phân cách bằng cầu thang ốp gỗ dẫn lên tầng lửng và hai lầu bên trên. Trong khi các tầng lầu 1 và 2 được bố trí các phòng ngủ và phòng làm việc thì trọn diện tích của tầng lửng dành cho phòng khách, nơi đáp ứng được cả hai mục tiêu vừa thoáng mát vừa kín đáo. Ngồi tại đây có thể cảm thấy được sự thông thoáng của không gian và tận hưởng được ánh sáng thiên nhiên chắt lọc từ giếng trời và cửa sổ rộng lớn bên hông và mặt trước. Cửa sổ rộng nhìn ra cầu thang và toàn ngôi nhà lại được khéo léo che lại bởi ba chiếc cửa sổ nhỏ xinh ở mặt tiền để tạo sự riêng tư cho không gian nơi đây.\
\
Tuy chỉ với màu trắng giản dị được nối kết bằng các màu nền đen hoặc nâu gỗ và những điểm xanh xuyên suốt căn nhà, kiến trúc sư đã xếp đặt và bố trí hài hòa giữa màu sắc và không gian nội thất tạo nên một cảm giá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 áo, bàn ăn và kệ và lúc nào cũng ánh lên những màu khác nhau theo mức độ phản chiếu ánh sáng. Điểm xuyết trên nền trắng này là màu xanh của nệm ghế hay các vật dụng trang trí nho nhỏ. Phải chăng cách xử lý khôn ngoan này đã làm bật lên nét trẻ trung cho không gian bên trong ngôi nhà và làm cho ngôi nhà trở nên nhiều màu sắc?\
\
Bốn tấc giếng trời bên hông nhà không những đã hữu dụng trong việc đem lại ánh sáng và gió cho phòng ăn, phòng khách và phòng làm việc mà còn tạo được nên nét độc đáo riêng cho ngôi nhà. Giếng trời bên hông kết hợp với lựa chọn các cửa phòng khách, cửa bước ra ban công và cửa sổ hầu như được mở rộng, kéo dài xuống tận sàn và gắn kính trong suốt đã mang lại nét thoáng mở và hiện đại cho từng không gian có sự hiện diện của giếng trời như phòng ăn dưới đất, phòng khách ở tầng lửng và phòng làm việc ở tầng lầu 1. Các đồ nội thất được thiết kế theo kiểu “tailor - made” (may đo) như kệ sách, bàn trang điểm, giường ngủ, gương trong phòng tắm, bàn ăn, bàn uống trà… theo những đường thẳng tắp cũng góp phần tạo nên nét hiện đại và đơn giản.\
\
Trong một thế giới mà hàng hiệu luôn chiếm lĩnh, việc tạo ra một phong cách cho ngôi nhà của mình theo sở thích riêng thật không dễ dàng chút nào. Thêm vào đó làm thế nào để gói gọn các ý tưởng của kiến trúc sự và nguyện vọng của chủ nhân trong một không gian hạn hẹp cũng là một thách thức. Vậy đó, sáng tạo, trẻ trung và hiện đại là thông điệp mà ngôi nhà bé xinh này muốn chuyển tải.\
', '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'),

I know why and how to deal with it. Can mod help me delete this useless post…

You could post the solution and make this a useful post for others that might have the same problem.

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.

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.

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

but isn’t this

PRIMARY KEY  (`id`)

in your original code setting the primary key on id?

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