<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Tutorial Blog &#187; update</title>
	<atom:link href="http://www.thetutorialblog.com/tag/update/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thetutorialblog.com</link>
	<description></description>
	<lastBuildDate>Wed, 07 Jul 2010 23:30:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<image>
<link>http://www.thetutorialblog.com</link>
<url>http://www.thetutorialblog.com/wp-content/plugins/maxblogpress-favicon/icons/favicon-28.ico</url>
<title>The Tutorial Blog</title>
</image>
		<item>
		<title>PHP News System Part 4</title>
		<link>http://www.thetutorialblog.com/php/php-news-system-part-4/</link>
		<comments>http://www.thetutorialblog.com/php/php-news-system-part-4/#comments</comments>
		<pubDate>Sun, 17 Aug 2008 23:15:05 +0000</pubDate>
		<dc:creator>Wez Pyke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Text Tutorials]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[edit]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[news system]]></category>
		<category><![CDATA[part 4]]></category>
		<category><![CDATA[post]]></category>
		<category><![CDATA[remove]]></category>
		<category><![CDATA[thetutorialblog]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://www.thetutorialblog.com/?p=34</guid>
		<description><![CDATA[	<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
		<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
		<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
		<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
		<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
		<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
		<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
	If you haven't checked out the previous tutorials then click on the names to view them. Part 1, Part 2, Part3. In Part 4 of this tutorial you are going to learn how to edit and delete your posts from the admin page. Connect to the database The first thing that we need to do [...]


Related posts:<ol><li><a href='http://www.thetutorialblog.com/php/uploading-files-to-rackspace-cloud-mosso-using-php-api/' rel='bookmark' title='Permanent Link: Uploading files to Rackspace Cloud (Mosso) using PHP API'>Uploading files to Rackspace Cloud (Mosso) using PHP API</a> <small>Rackspace Cloud is a great VPS service, but when I...</small></li>
<li><a href='http://www.thetutorialblog.com/php/adding-security-to-codeigniter-forms-with-a-custom-library-class/' rel='bookmark' title='Permanent Link: Adding security to CodeIgniter forms with a custom library class'>Adding security to CodeIgniter forms with a custom library class</a> <small>The class that we are going to create within CI...</small></li>
<li><a href='http://www.thetutorialblog.com/php/twitter-like-pagination-using-codeigniter-and-jquery/' rel='bookmark' title='Permanent Link: Twitter-like pagination using CodeIgniter and jQuery'>Twitter-like pagination using CodeIgniter and jQuery</a> <small>If you use Twitter you'll notice that there is a...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[	<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
		<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
		<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
		<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
	<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.thetutorialblog.com%2Fphp%2Fphp-news-system-part-4%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.thetutorialblog.com%2Fphp%2Fphp-news-system-part-4%2F&amp;source=wezpyke&amp;style=normal&amp;service=bit.ly" height="61" width="50" title="PHP News System Part 4" alt=" PHP News System Part 4" /><br />
			</a>
		</div>
<p>If you haven't checked out the previous tutorials then click on the names to view them. <a href="http://www.thetutorialblog.com/2008/06/17/php-news-system-part-1-with-sound/">Part 1</a>, <a href="http://www.thetutorialblog.com/2008/07/30/php-news-system-part-2/">Part 2</a>, <a href="http://www.thetutorialblog.com/2008/08/01/php-news-system-part-3/">Part3</a>.</p>
<p>In Part 4 of this tutorial you are going to learn how to edit and delete your posts from the admin page.<br />
<span id="more-34"></span></p>
<h2>Connect to the database</h2>
<p>The first thing that we need to do is go to <strong>index.php</strong> under the admin folder and to the after the first <strong>&lt;?php </strong>add this:</p>
<p><div id="code_div">include("../dbconnect.php");</div></p>
<h2>Display The Posts</h2>
<p>After this line in index.php<br />
<div id="code_div">echo "&lt;a href="newpost.php">New Post&lt;/a>";</div></p>
<p>add below it<br />
<div id="code_div">echo "&lt;br>";<br />
		echo "&lt;br>";<br />
		echo "Edit Posts &lt;br>";</p>
<p>		$query = mysql_query("SELECT * FROM news_system ORDER BY id DESC");<br />
		while ( $row = mysql_fetch_array( $query ) ) {<br />
			$id = $row['id'];<br />
			$author = $row['author'];<br />
			$title = $row['title'];</p>
<p>			echo '&lt;a href="editpost.php?id='.$id.'">'.$title.' - By '.$author.'&lt;/a>&lt;br>';<br />
		}<br />
	}<br />
</div></p>
<p>What we're doing here is querying the database to display all the posts in the table <em>news_system</em> from the newest post to the oldest post. We then use a while loop to loop through and display each post. You will see that each post links to <strong>editpost.php</strong> so what we will do now is create an <strong>editpost.php</strong> file.</p>
<h2>Creating editpost.php</h2>
<p>After you have created your editpost.php file add the following code to it<br />
<div id="code_div">&lt;?php<br />
include("../dbconnect.php");</p>
<p>$id = $_GET['id'];</p>
<p>?></div></p>
<p>What we're doing here is opening the php tags then including <strong>dbconnect.php</strong> that connects to the database. <em>$_GET['id']</em> gets the id from the URL and stores it in a variable called <em>$id</em>. Then we close the php tags with <em>?></em>.</p>
<p>Before the <em>?></em> insert the following code.<br />
<div id="code_div">$query = mysql_query("SELECT * FROM news_system WHERE id='$id'");<br />
$row = mysql_fetch_array($query);</p>
<p>$author = $row['author'];<br />
$date = $row['date'];<br />
$title = $row['title'];<br />
$post = $row['post'];</div><br />
What this code will do is find the row in the table <em>news_system</em> with the id that is stored in $id. Then we retrieve the author, date, title and post information.</p>
<p>Below this we're going to create a html form and display the information that we have retrieved from the database within it so that we can edit it.</p>
<p><div id="code_div">if( !$_POST['submit'] ) {<br />
	echo '&lt;form method="POST">';<br />
	echo 'Author: &lt;input type="text" name="author" value="'.$author.'"> &lt;br>';<br />
	echo 'Date: &lt;input type="text" name="date" value="'.$date.'"> &lt;br>';<br />
	echo 'Title: &lt;input type="text" name="title" value="'.$title.'"> &lt;br>';<br />
	echo 'Post: &lt;br>&lt;textarea name="post" cols="30" rows="5">'.$post.'&#038;lt'/textarea>&lt;br>';<br />
	echo '&lt;input type="hidden" name="id" value="'.$id.'">';<br />
	echo '&lt;input type="submit" name="submit" value="Edit Post">&lt;br>&lt;br>';</p>
<p>	echo '&lt;a href="deletepost.php?id='.$id.'">Delete Post&lt;/a>';<br />
} </div></p>
<p>What happens in this piece of code is if the post hasn't been submitted then include the form. Within all the form elements you'll see that they all have <em>value</em> and that value is the variables. We also include the id in a hidden element so that we can carry it over for when the submit button is pressed. Under the submit button there is a link to a page that we will create in a minute that will delete that post.</p>
<p>In the next lot of code we're going to make it retrieve all of the values from the form and store them in variables that will then be used to update the row within the table.<br />
<div id="code_div">else {<br />
	$id = $_POST['id'];<br />
	$author = $_POST['post'];<br />
	$date = $_POST['date'];<br />
	$title = $_POST['title'];<br />
	$post = $_POST['post'];</p>
<p>	$query = mysql_query("UPDATE news_system SET author='$author', date='$date', title='$title', post='$post' WHERE id='$id'");<br />
	echo 'Post was updated, &lt;a href="index.php">return home</a>';<br />
}</div><br />
Here you can see we are retrieving all the information from the form and storing the information into variables. Then we query the database to update the new values and lastly return a message to the user to know that it has been updated.</p>
<p>All the code together should look like this:<br />
<div id="code_div"><br />
&lt;?php<br />
include("../dbconnect.php");</p>
<p>$id = $_GET['id'];</p>
<p>$query = mysql_query("SELECT * FROM news_system WHERE id='$id'");<br />
$row = mysql_fetch_array($query);</p>
<p>$author = $row['author'];<br />
$date = $row['date'];<br />
$title = $row['title'];<br />
$post = $row['post'];</p>
<p>if( !$_POST['submit'] ) {<br />
	echo '&lt;form method="POST">';<br />
	echo 'Author: &lt;input type="text" name="author" value="'.$author.'"> &lt;br>';<br />
	echo 'Date: &lt;input type="text" name="date" value="'.$date.'"> &lt;br>';<br />
	echo 'Title: &lt;input type="text" name="title" value="'.$title.'"> &lt;br>';<br />
	echo 'Post: &lt;br>&lt;textarea name="post" cols="30" rows="5">'.$post.'&#038;lt'/textarea>&lt;br>';<br />
	echo '&lt;input type="hidden" name="id" value="'.$id.'">';<br />
	echo '&lt;input type="submit" name="submit" value="Edit Post">&lt;br>&lt;br>';</p>
<p>	echo '&lt;a href="deletepost.php?id='.$id.'">Delete Post&lt;/a>';<br />
} </p>
<p>else {<br />
	$id = $_POST['id'];<br />
	$author = $_POST['post'];<br />
	$date = $_POST['date'];<br />
	$title = $_POST['title'];<br />
	$post = $_POST['post'];</p>
<p>	$query = mysql_query("UPDATE news_system SET author='$author', date='$date', title='$title', post='$post' WHERE id='$id'");<br />
	echo 'Post was updated, &lt;a href="index.php">return home</a>';<br />
}<br />
?><br />
</div></p>
<p>Now that we have created the <strong>editpost.php</strong> file its time to create <strong>deletepost.php</strong>.</p>
<h2>Delete a post</h2>
<p>In this delete file I haven't added any confirmation to ask the user if they're certain that they want to delete the post. It might be a good idea to add it using submit buttons or javascript.</p>
<p><div id="code_div">&lt;?php<br />
include("../dbconnect.php");<br />
$id = $_GET['id'];</p>
<p>$query = mysql_query("DELETE FROM news_system WHERE id='$id'");</p>
<p>echo 'The post was deleted, &lt;a href="index.php">return home&lt;/a>';<br />
?></div></p>
<p>As usual we include the php tag and the database file as we're going to be querying the database. We then get the id from the URL and store it in <em>$id</em>. Then we query the database and delete the post and last of all we display a message to the user saying the post has been deleted.</p>
<h2>Thank you</h2>
<p>Thanks for taking the time to read this tutorial. I hope that you have learnt something and that you keep returning to check out our latest tutorials.</p>
<p>If there is anything that you need help with then please post a comment and I'll write a tutorial on it or get back to you.</p>


<p>Related posts:<ol><li><a href='http://www.thetutorialblog.com/php/uploading-files-to-rackspace-cloud-mosso-using-php-api/' rel='bookmark' title='Permanent Link: Uploading files to Rackspace Cloud (Mosso) using PHP API'>Uploading files to Rackspace Cloud (Mosso) using PHP API</a> <small>Rackspace Cloud is a great VPS service, but when I...</small></li>
<li><a href='http://www.thetutorialblog.com/php/adding-security-to-codeigniter-forms-with-a-custom-library-class/' rel='bookmark' title='Permanent Link: Adding security to CodeIgniter forms with a custom library class'>Adding security to CodeIgniter forms with a custom library class</a> <small>The class that we are going to create within CI...</small></li>
<li><a href='http://www.thetutorialblog.com/php/twitter-like-pagination-using-codeigniter-and-jquery/' rel='bookmark' title='Permanent Link: Twitter-like pagination using CodeIgniter and jQuery'>Twitter-like pagination using CodeIgniter and jQuery</a> <small>If you use Twitter you'll notice that there is a...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.thetutorialblog.com/php/php-news-system-part-4/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
