0

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');
         }


          ?>
4

3 回答 3

1

You're not using ON DUPLICATE KEY properly. There's no opportunity for a duplicate key to occur because you're not providing an ID field in your insert statement that could conflict.

You need to decide on something that is unique to an RSS feed and make this the primary key. If guid is indeed a true GUID, that would be ideal. Otherwise, using link could work, but you'll probably find that it's too long to use as a PK.

Edit

ID isn't related to the data which makes it impossible to use for checking for duplicates. To use link, make that the primary key:

ALTER TABLE rss_feeds DROP PRIMARY KEY, ADD PRIMARY KEY (link);

Note that the maximum length for a PK is 767 bytes in InnoDB and 1000 in MyISAM. Depending on the encoding of the column, it may use up multiple bytes per character.

You should then change your query to this and retry:

INSERT INTO rss_feeds (title, link, description, pubdate, guid) VALUES ('$titlefield','$linkfield','$desfield','$pubs','$guids')
    ON DUPLICATE KEY UPDATE title = '$titlefield', description ='$desfield', pubdate ='$pubs', guid ='$guids';
于 2013-09-13T01:40:41.843 回答
0

Compare new RSS with existing RSS stored in database. If it exists than update the values otherwise insert the new RSS.

于 2013-09-13T01:35:36.557 回答
0

According to your query you must have primary or unique key from title, link, description, pubdate, guid and I think Either link or guid will be the best bet for this. You have to check which field is coming unique in feed.

于 2013-09-13T01:40:34.267 回答