PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP PHP News CMS

PHP News CMS


PHP News CMSThis Tutorial is a continuation of our PHP News CMS Tutorial. If you have not yet taken the first part of this tutorial, please go take it now. In part one of this tutorial, you learned how to setup a working CMS that allows you to add news or other content to your website. But what do we do if we need to go back and edit or delete one of our posts? This part of the tutorial will show you how to edit and delete previous entries.

1. First, be sure that you have successfully completed part one of this tutorial, and that you are able to post entries with your CMS. Once this is done, proceed to step 2.

2.So far, you should have the following files in your basic CMS:

- dbconnect.php (Connects to the database)
- viewnews.php (Displays entries posted into database)
- submit.php (Page to post entries into database)

Now, create a new file called editnews.php. In this file, you will script a way to edit and delete previous posts. In editnews.php, start with the following code:

<?php
include("dbconnect.php"); //connect to the database

if(!isset($cmd)) //if the variable $cmd is not set, do this
{
//display all the news
$result = mysql_query("select * from mynews order by id DESC"); //select all posts from table mynews
while($r=mysql_fetch_array($result)) //for every row in the table, do this
{
extract($r);//remove the $r, so that its just the variable

//display each entry with links to edit and delete them
echo "<b><u>$title</b></u>
".stripslashes($message)."
By $user
<a href='editnews.php?cmd=edit&id=$id'> Edit< /a> :: :: <a href='editnews.php?cmd=delete&id=$id'> Delete </a>
";
echo "
";//so we have spacing between entries
}//end functions
}
?>

3. This file is not done yet. What this file does so far, is lists the news entries much like viewnews.php does. At the bottom of every post, it also displays a link to edit and delete the news. Go back into your document, and after the ?> add the following code:

<?php
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") //if the link for the edit cmd was selected, do this
{
if (!isset($_POST["submit"])) //if submit has not yet been pressed, do this
{
$id = $_GET["id"]; //get the id of the news article being edited. This is provided in the link to edit it.
$sql = "SELECT * FROM mynews WHERE id=$id"; //select all columns from mynews with this id
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>

<form action="editnews.php" method="post">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">

Title*:

<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30>

Message*:

<TEXTAREA NAME="message" rows=10 cols=43><? echo $myrow["message"] ?>

User*:

<INPUT TYPE="TEXT" NAME="user" VALUE="<?php echo $myrow["user"] ?>" SIZE=30>

<input type="hidden" name="cmd" value="edit">

<input type="submit" name="submit" value="submit">

</form>

<? } ?>

<?php
if ($_POST["$submit"]) //if submit was pressed, do this
{
$title = $_POST["title"];
$message = $_POST["message"];
$user = $_POST["user"];
$email = $_POST["email"];

$sql = "UPDATE mynews SET title='$title',message='$message',user='$user',email='$email' WHERE id=$id"; //UPDATE the columns in your table with the new content provided in the text areas in the form

$result = mysql_query($sql); //run this query
echo "Information updated. <a href=editnews.php>Return</a>"; //provide an easy link back to editnews.php
}
}
?>

4. That is all there is to editing your posts! Now we need to finish up this CMS with a way to delete old entries. Go back to editnews.php, and add the following code:

<?php
if($_GET["cmd"]=="delete")
{
$sql = "DELETE FROM mynews WHERE id=$id"; //DELETE the row that contains the $id of the post selected
$result = mysql_query($sql); //run query
echo "News Article Deleted!
<a href=editnews.php>Return to Edit News</a>"; //easy link back to editnews.php
}
?>

And thats it! You now have a working CMS that allows you to Add, Update/Edit, and Delete entries! Happy posting, and good luck!



Author's URL: GreyCobra.com
PHP is open source scripting language. It\'s widely used to develop web applications. More PHP Tutorials: Featured Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

No comments yet...
Add comments to "PHP News CMS"

Captcha