Reputation: 509
I'm reading and store rss feed in my database ,my requirement is if link field is same then update title and description only in database.
I'm using ON DUPLICATE KEY UPDATE but after entered query.its insert only a single data in database and failed and show a error
my code is
<?php
include_once 'db.php';
$homepage = file_get_contents('http://rss.cnn.com/rss/edition_us.rss');
$movies = new SimpleXMLElement($homepage);
foreach($movies->channel->item as $opt){
$title= $opt->title;
$tittle=mysql_real_escape_string($title);
$link=$opt->link;
$links=mysql_real_escape_string($link);
$des=$opt->description;
$dess=mysql_real_escape_string($des);
"INSERT INTO store_feed (title, link, description) VALUES ('$tittle','$links','$dess')";
$sql="ON DUPLICATE KEY UPDATE title= '$tittle',description='$dess'";
$result=mysql_query($sql) or die( mysql_error() );
}
?>
and table structure is:-
CREATE TABLE `test_om`.`store_feed` (
`id` INT NOT NULL AUTO_INCREMENT ,
`title` VARCHAR( 200 ) NOT NULL ,
`link` VARCHAR( 200 ) NOT NULL ,
`description` VARCHAR( 500 ) NOT NULL ,
`feedburner` VARCHAR( 200 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
UNIQUE (
UNIQUE KEY `link` (`link`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
and error is Duplicate entry 'http://rss.cnn.com/~r/rss/edition_us/~3/Wl1V4JsNqDU/index.html' for key 'link'
I have done some changes in my query ,short it.
<?php
include_once 'db.php';//config file
$homepage = file_get_contents('http://rss.cnn.com/rss/edition_us.rss');//link of rss feed page
$movies = new SimpleXMLElement($homepage);
foreach($movies->channel->item as $opt){
$sql="INSERT INTO store_feed SET `title` = '".mysql_real_escape_string($opt- >title)."',
`link`='".mysql_real_escape_string($opt->link)."',
`description`='".mysql_real_escape_string($opt->description)."'"
."ON DUPLICATE KEY UPDATE `title`='".mysql_real_escape_string($opt->title).
"',`description`='".mysql_real_escape_string($opt->description)."'";
$result=mysql_query($sql) or die( mysql_error() );
}
?>
and its solve my problem....
Upvotes: 1
Views: 421
Reputation: 16115
INSERT ... ON DUPLICATE has to be one query:
$sql="INSERT INTO store_feed (title, link, description) VALUES ('$tittle','$links','$dess')"
." ON DUPLICATE KEY UPDATE title= '$tittle',description='$dess'";
$result=mysql_query($sql) or die( mysql_error() );
Upvotes: 3