troubleshooting Question

Saving XML to Database using PHP

Avatar of rhiannon1
rhiannon1 asked on
PHPXMLMySQL Server
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.  
<?php
/**
 * 
 * 
 **/
session_start();
/* --- 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");
 
try
{
    /*  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 http://www.medicalnewstoday.com */
    $rss_document = simpleXML_load_file('http://www.medicalnewstoday.com/rss/smoking.xml');
 
    /* 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("<h2>$rss_title</h2>");
    $page->addToBody("<h3><a href='" . $rss_link . "'>{$rss_link}</a></h3>");
    $page->addToBody("<h3>$rss_editor</h3>");
    $page->addToBody("<h3>$rss_copyright</h3>");
    $page->addToBody("</div>");
    /* -------------------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>");
    $page->addToBody("</div>");
 
    /* ------------------Valid XHTML Image----------------- */
    $page->addToBody("<p><a href='http://validator.w3.org/check?uri=referer'><img
        src='http://www.w3.org/Icons/valid-xhtml10-blue'
        alt='Valid XHTML 1.0 Transitional' height='31' width='88' /></a></p>");
    $page->addToBody("<div class='separator'></div>");
    /* ---------------------------------------------------- */
 
    /* ------------------Valid CSS image------------------- */
    $page->addToBody("<p>
<a href='http://jigsaw.w3.org/css-validator/'>
    <img src='http://jigsaw.w3.org/css-validator/images/vcss-blue'
        alt='Valid CSS!' /></a></p>");
    /* ---------------------------------------------------- */
 
    $page->addToBody("</div>");
    /* ------------------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();
 
    $page->addToBody("</div></div>");
    /*-------------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