troubleshooting Question

Saving XML to Database using PHP

Avatar of rhiannon1
rhiannon1 asked on
13 Comments1 Solution506 ViewsLast Modified:
Hi, need some advice/help with saving a parsed RSS feed to my mysql database.

I am parseing, and then displaying the external RSS feed on my page, and now i want to beable to click a link after each article, which will save the article to my databse, which can be retrived at a later date.

How can i do this?  I have been looking at some code in books, and it showed a little snippet , putting variables in a link followed by &nbsp. I have done this, so now it shows, save article on my page, and when you hover over the link, it shows a big long script with the values of the variables. But how do i get from this, to saving to the database?

I have created the SQL statement ... $sqlSAVE.  I am probably being stupid, and it is very simple, byt i am scratching my head trying to figure this out.  
/* --- If session data is correct, then Welcome to secure area, else back to the login page --- */
if (isset($_SESSION['email']) && isset($_SESSION['password']))
} else
    header("Location: logformTest.php");
/** require the webpage class definition file also the database connection file. */
require_once ("web.class.php");
require_once ("dbconnect.php");
    /*  query the database */
    $db = getConnection();
    /* ------ End of Querys ------- */
    /* create a new instance of the webpage class passing the title and an array of stylesheets to the constructor */
    $page = new Webpage('Smoking RSS Feed', array('css/frfrh66.css'));
    /* Parse XML from external RSS feed */
    $rss_document = simpleXML_load_file('');
    /* Get title, link, managing editor, and copyright from the document and store them in variables */
    $rss_title = $rss_document->channel->title;
    $rss_link = $rss_document->channel->link;
    $rss_editor = $rss_document->channel->managingEditor;
    $rss_copyright = $rss_document->channel->copyright;
    /* -------------------Header Area-------------------- */
    $page->addToBody("<div id='header'>");
    $page->addToBody("<img src='images/StopSmoking1.jpg' alt='Stop Smoking' width='200' height='90' border='0' class='img' />");
    $page->addToBody("<h3><a href='" . $rss_link . "'>{$rss_link}</a></h3>");
    /* -------------------End of Header------------------- */
    /* -------------------Menu Div's --------------------- */
    $page->addToBody("<div id='lsidebar'>");
    $page->addToBody("<div class='nmenu'>");
    $page->addToBody("<a class='topgap' href='home1.php'>News Article List</a>");
    $page->addToBody("<a class='btmgap' href='rssNeuroScience.php'>Neuro Science</a>");
    $page->addToBody("<a class='btmgap' href='searchRSS.php'>Search Articles</a>");
	$page->addToBody("<a class='btmgap' href='savedRSS.php'>Saved Articles</a>");
    $page->addToBody("<a class='btmgap' href='logout.php'>Logout</a>");
    $page->addToBody("<div class='separator'></div>");
    /* ------------------Valid XHTML Image----------------- */
    $page->addToBody("<p><a href=''><img
        alt='Valid XHTML 1.0 Transitional' height='31' width='88' /></a></p>");
    $page->addToBody("<div class='separator'></div>");
    /* ---------------------------------------------------- */
    /* ------------------Valid CSS image------------------- */
<a href=''>
    <img src=''
        alt='Valid CSS!' /></a></p>");
    /* ---------------------------------------------------- */
    /* ------------------End of Menu Div's----------------- */
    /* ------------------Main Content Area----------------- */
    $page->addToBody("<div id='contentarea'>");
    $page->addToBody("<div class='content'>");
    /* Display XML articles on my page */
    //Loop through each item in the RSS document
    foreach ($rss_document->channel->item as $RSSitem)
        // modify string from articles to printer friendly news //
        $printer = str_replace(array('.php', 'articles/'), array('',
            'printerfriendlynews.php?newsid='), $RSSitem->link);
				/* Create variables for saving articles to database */
		$subscriberID = $_SESSION['email'];
		$link = $printer;
		$title = $RSSitem->title;
		$category = $RSSitem->category;
    	$body1 = file_get_contents($printer);
    	$body2 = strip_tags($body1);
        //The variable $RSSitem will hold a different item for each loop
        $page->addToBody("<b><a href='$printer' target='blank'> $RSSitem->title </a></b><br />");
        $page->addToBody("<b>$RSSitem->pubDate</b><br />");
        $page->addToBody("$RSSitem->description<br />");
        $page->addToBody("[<a href='$subscriberID&nbsp;$link&nbsp;$title&nbsp;$category&nbsp;$body2&nbsp;' target='blank'> SAVE ARTICLE </a>]<br /><br />");
		/* Sql statement for saveing articles to the database */
        $sqlSAVE = ("INSERT INTO article (subscriberID, link, title, category, body)  VALUES ('$subscriberID','$printer','$title','$category','$body2')" );
		/* Prepare and execute $sqlSAVE */
		//$insertSQL = $db->prepare($sqlSAVE);
		//$insertSQL ->execute();
    /*-------------End of Main Content Area---------------- */
    /* Call the getPage function from the webpage class, which will display the head, body and footer. */
    echo $page->getPage();
catch (PDOException$e)
    echo $e->getMessage();
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 13 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros