Thursday, 12 September 2013

duplicate key update php not working

duplicate key update php not working

On searching the forum, I wasn't able to find exactly what I looking for
(so sorry of it's a duplicate) ...I'm new to PHP, but I was able to get an
rss feed's items into a database (MYSQL).. each time this code (below)
runs it adds the same items into the database, instead of updating items
with the same link. ID field(int) is the primary key... appreciate any
help:
<?php
$calendar = file_get_contents('feed.rss');
$entries = new SimpleXMLElement($calendar);
foreach($entries->channel->item as $items){
$title= $items->title;
$titlefield=mysql_real_escape_string($title);
$des=$items->description;
$desfield=mysql_real_escape_string($des);
$link=$items->link;
$linkfield=mysql_real_escape_string($link);
$pubdate=$items->pubDate;
$pubs=mysql_real_escape_string($pubdate);
$guid=$items->guid;
$guids=mysql_real_escape_string($guid);
$rss="INSERT INTO rss_feeds (title, link, description, pubdate,
guid) VALUES ('$titlefield','$linkfield','$desfield','$pubs'
,'$guids')"
." ON DUPLICATE KEY UPDATE title = '$titlefield', link = '$linkfield',
description ='$desfield', pubdate ='$pubs', guid ='$guids'";
$result=mysql_query($rss) or die('Error, insert query failed');
}
?>

No comments:

Post a Comment