PHP News System Part 4

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 is go to index.php under the admin folder and to the after the first <?php add this:

include("../dbconnect.php");

Display The Posts

After this line in index.php

echo "<a href="newpost.php">New Post</a>";

add below it

echo "<br>";
echo "<br>";
echo "Edit Posts <br>";

$query = mysql_query("SELECT * FROM news_system ORDER BY id DESC");
while ( $row = mysql_fetch_array( $query ) ) {
$id = $row['id'];
$author = $row['author'];
$title = $row['title'];

echo '<a href="editpost.php?id='.$id.'">'.$title.' - By '.$author.'</a><br>';
}
}

What we're doing here is querying the database to display all the posts in the table news_system 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 editpost.php so what we will do now is create an editpost.php file.

Creating editpost.php

After you have created your editpost.php file add the following code to it

<?php
include("../dbconnect.php");

$id = $_GET['id'];

?>

What we're doing here is opening the php tags then including dbconnect.php that connects to the database. $_GET['id'] gets the id from the URL and stores it in a variable called $id. Then we close the php tags with ?>.

Before the ?> insert the following code.

$query = mysql_query("SELECT * FROM news_system WHERE id='$id'");
$row = mysql_fetch_array($query);

$author = $row['author'];
$date = $row['date'];
$title = $row['title'];
$post = $row['post'];


What this code will do is find the row in the table news_system with the id that is stored in $id. Then we retrieve the author, date, title and post information.

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.

if( !$_POST['submit'] ) {
echo '<form method="POST">';
echo 'Author: <input type="text" name="author" value="'.$author.'"> <br>';
echo 'Date: <input type="text" name="date" value="'.$date.'"> <br>';
echo 'Title: <input type="text" name="title" value="'.$title.'"> <br>';
echo 'Post: <br><textarea name="post" cols="30" rows="5">'.$post.'&lt'/textarea><br>';
echo '<input type="hidden" name="id" value="'.$id.'">';
echo '<input type="submit" name="submit" value="Edit Post"><br><br>';

echo '<a href="deletepost.php?id='.$id.'">Delete Post</a>';
}

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 value 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.

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.

else {
$id = $_POST['id'];
$author = $_POST['post'];
$date = $_POST['date'];
$title = $_POST['title'];
$post = $_POST['post'];

$query = mysql_query("UPDATE news_system SET author='$author', date='$date', title='$title', post='$post' WHERE id='$id'");
echo 'Post was updated, <a href="index.php">return home';
}


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.

All the code together should look like this:


<?php
include("../dbconnect.php");

$id = $_GET['id'];

$query = mysql_query("SELECT * FROM news_system WHERE id='$id'");
$row = mysql_fetch_array($query);

$author = $row['author'];
$date = $row['date'];
$title = $row['title'];
$post = $row['post'];

if( !$_POST['submit'] ) {
echo '<form method="POST">';
echo 'Author: <input type="text" name="author" value="'.$author.'"> <br>';
echo 'Date: <input type="text" name="date" value="'.$date.'"> <br>';
echo 'Title: <input type="text" name="title" value="'.$title.'"> <br>';
echo 'Post: <br><textarea name="post" cols="30" rows="5">'.$post.'&lt'/textarea><br>';
echo '<input type="hidden" name="id" value="'.$id.'">';
echo '<input type="submit" name="submit" value="Edit Post"><br><br>';

echo '<a href="deletepost.php?id='.$id.'">Delete Post</a>';
}

else {
$id = $_POST['id'];
$author = $_POST['post'];
$date = $_POST['date'];
$title = $_POST['title'];
$post = $_POST['post'];

$query = mysql_query("UPDATE news_system SET author='$author', date='$date', title='$title', post='$post' WHERE id='$id'");
echo 'Post was updated, <a href="index.php">return home';
}
?>

Now that we have created the editpost.php file its time to create deletepost.php.

Delete a post

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.

<?php
include("../dbconnect.php");
$id = $_GET['id'];

$query = mysql_query("DELETE FROM news_system WHERE id='$id'");

echo 'The post was deleted, <a href="index.php">return home</a>';
?>

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 $id. 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.

Thank you

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.

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.

Bookmark and Share
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Ping.fm
  • Tumblr
  • Twitter

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

, , , , , , , , , , , ,

5 Responses to PHP News System Part 4

  1. Sorinu September 9, 2008 at 6:44 am #

    Hi, can u put all files to download, i mean full code? not code on parts, please, or the code of all pages if u can.

    Thanks a lot!

  2. PAL December 18, 2008 at 1:08 pm #

    Hey! Nice tutorial. It's very helpful for me. Can I have full code of all parts. I have an error and can't understand what's wrong.

  3. Dilson December 28, 2008 at 9:20 am #

    very nice tutorial and easy to understand. thanks. hope you can make more tutorials on php/mysql

  4. TriplebTalk March 14, 2009 at 5:28 pm #

    How are we meant to see the previous 5 posts? Do they disappear aafterwards?

  5. John April 27, 2009 at 4:47 pm #

    Nice, but some source files would really help. I got lost half way through and it took forever to fix. With a source, at least there's something to compare to.

Leave a Reply