Posted on December 21st, 2006 05:09 PM by Matt

Content Management System: Part 2
Difficulty: Advanced
Type: php
Rate this tutorial: You must be logged in to rate tutorials
Rating: with 1 votes





Part 2 of the CMS tutorial, you can find part 1 <A href="tutorials.php?id=20&job="view_item">here</A>.
This tutorial will cover:
    1)Deleting Information
    2)Editing Information

The Code we have so far in index.php:


<?php
/*
Tutorial Copyright 2006 - 2007
Matt Davenport - www.theflashplace.com
*/

include("connection.php"); // connect to the database
$job = $_REQUEST['job']; // this is now needed because there will be many different options for the data

if(!$job){ //if variable job isn't set
//this is an sql statement that will go through the database and select the title, date and content of the news items.
$searchSql = mysql_query("SELECT newsTitle,newsDate,newsContent FROM `news`");

$newsItemsInDatabase = mysql_num_rows($searchSql); //check to see how many rows there are

if($newsItemsInDatabase == 0){ // if there are 0 then:
    echo('Sorry, there are currently no news items!'); // display this message
}else{
    while ($search = mysql_fetch_array($searchSql, MYSQL_ASSOC))// if there are some then retrieve the information
    {
        $newsTitle = $search["newsTitle"]; //retrieve title
        $newsDate = $search["newsDate"]; //retrieve date
        $newsContent = $search["newsContent"]; //retrieve content
        echo($newsTitle.' - '.$newsDate.'<br>'.$newsContent.'<br><br>'); //display the results clearly
    }

};
}

switch ($job) //this saves us from writing thousands of if statements!
{
case "add_news" :
    // the next part just places the basic form onto the page
    echo('
    <form name="add_news" method="post" action="index.php?job=confirm_add_news">
    Title: <input type="text" name="title">    <br>
    Content: <TEXTAREA name="content" ROWS=10 COLS=70></TEXTAREA> <br>
    </form>');
break; //exit this case statement

case "confirm_add_news" :
    $date = date("F dS, Y h:i A"); // get todays date using phps date(); function
    $title = $_POST['title']; // get the title from the form
    $content = $_POST['content']; // get the content from the form
    $content = nl2br($content); // convert any space between lines into <br>
    
    // then insert this information into the mySQl database
    mysql_query("INSERT INTO news (newsTitle, newsDate, newsContent) VALUES ('$title', '$date', '$content')") or die(mysql_error());
    
    //users will then see this and can then go back to the main page
    die('Thank-you for submitting this news item, click <A href="index.php">here</A> to go back');
break;

    
}// end switch
?>



Now we have to add the "delete function, so underneath the last "break;"
we need a new case "delete_news" so that users can delete any news items, we will also need to get the ID when looping through the database

and add a "[delete news]" selection, this will send a request to the page with the news ID needing to be deleted and the job variable equal

to "delete_news"


while ($search = mysql_fetch_array($searchSql, MYSQL_ASSOC))// if there are some then retrieve the information
    {
        $newsTitle = $search["newsTitle"]; //retrieve title
        $newsDate = $search["newsDate"]; //retrieve date
        $newsContent = $search["newsContent"]; //retrieve content
        echo($newsTitle.' - '.$newsDate.'<br>'.$newsContent.'<br><br>'); //display the results clearly
    }



will now change to



while ($search = mysql_fetch_array($searchSql, MYSQL_ASSOC))// if there are some then retrieve the information
    {
        $newsID = $search["id"]; //get the id of the news item
        $newsTitle = $search["newsTitle"]; //retrieve title
        $newsDate = $search["newsDate"]; //retrieve date
        $newsContent = $search["newsContent"]; //retrieve content
        echo($newsTitle.' - '.$newsDate.'<br>'.$newsContent.'<br><A href="index.php?job=delete_news&id='.$newsID.'">[Delete

News]</A><br>'); //display the results clearly
    }



now the "delete_news" case
You need to grab the information from the database with the corresponding ID
We need an "are you sure" screen incase the button was pressed accidentally!


case "delete_news" :
    $id = $_REQUEST['id']; //get the ID of the news article we're deleting
    $deletingSql = mysql_query("SELECT newsTitle FROM `news` WHERE id = $id"); //get the title of this news
    $deleting = mysql_fetch_array($deletingSql, MYSQL_ASSOC);
    $title = $deleting["title"]; //get the title of the to-be-deleted news article

    //now echo a simple for with "confirm" or "cancel"
    // if confirm is hit it will be sent to the case "confirm_delete_news"
    echo('Are you sure you want to delete News item - "<i>'.$title.'</i>"?
        <form action="index.php?job=confirm_delete_news&id='.$id.'" method="POST">
         <input type="submit" class="button_plain" value="confirm">
         &nbsp;<input type="button" class="button_plain" name="cancel" value="cancel" onClick="javascript:history.back()">
    ');
break;



That's the "delete_news" case that we need, but we also now need to actually delete the data from the database!
so we create another case called "confirm_delete_news" this will remove the information from the database and return some text to the user

confirming success.


case "confirm_delete_news" :
$id = $_REQUEST['id']; // get the id again
    mysql_query("DELETE FROM `news` WHERE `id` = '$id'"); //delete the row with the corresponding id from the database
    die('Item successfully deleted!<br>Click <A href="index.php">here</A> to go back to the main page');

break;



VOILA!
now let's get the code we have so far!


<?php
/*
Tutorial Copyright 2006 - 2007
Matt Davenport - www.theflashplace.com
*/

include("connection.php"); // connect to the database
$job = $_REQUEST['job']; // this is now needed because there will be many different options for the data

if(!$job){ //if variable job isn't set
//this is an sql statement that will go through the database and select the title, date and content of the news items.
$searchSql = mysql_query("SELECT newsTitle,newsDate,newsContent FROM `news`");

$newsItemsInDatabase = mysql_num_rows($searchSql); //check to see how many rows there are

if($newsItemsInDatabase == 0){ // if there are 0 then:
    echo('Sorry, there are currently no news items!'); // display this message
}else{
    while ($search = mysql_fetch_array($searchSql, MYSQL_ASSOC))// if there are some then retrieve the information
    {
        $newsID = $search["id"]; //get the id of the news item
        $newsTitle = $search["newsTitle"]; //retrieve title
        $newsDate = $search["newsDate"]; //retrieve date
        $newsContent = $search["newsContent"]; //retrieve content
        echo($newsTitle.' - '.$newsDate.'<br>'.$newsContent.'<br><A href="index.php?job=delete_news&id='.$newsID.'">[Delete

News]</A><br>'); //display the results clearly
    }

};
}

switch ($job) //this saves us from writing thousands of if statements!
{
case "add_news" :
    // the next part just places the basic form onto the page
    echo('
    <form name="add_news" method="post" action="index.php?job=confirm_add_news">
    Title: <input type="text" name="title">    <br>
    Content: <TEXTAREA name="content" ROWS=10 COLS=70></TEXTAREA> <br>
    </form>');
break; //exit this case statement

case "confirm_add_news" :
    $date = date("F dS, Y h:i A"); // get todays date using phps date(); function
    $title = $_POST['title']; // get the title from the form
    $content = $_POST['content']; // get the content from the form
    $content = nl2br($content); // convert any space between lines into <br>
    
    // then insert this information into the mySQl database
    mysql_query("INSERT INTO news (newsTitle, newsDate, newsContent) VALUES ('$title', '$date', '$content')") or die(mysql_error());
    
    //users will then see this and can then go back to the main page
    die('Thank-you for submitting this news item, click <A href="index.php">here</A> to go back');
break;

case "delete_news" :
    $id = $_REQUEST['id']; //get the ID of the news article we're deleting
    $deletingSql = mysql_query("SELECT newsTitle FROM `news` WHERE id = $id"); //get the title of this news
    $deleting = mysql_fetch_array($deletingSql, MYSQL_ASSOC);
    $title = $deleting["title"]; //get the title of the to-be-deleted news article

    //now echo a simple for with "confirm" or "cancel"
    // if confirm is hit it will be sent to the case "confirm_delete_news"
    echo('Are you sure you want to delete News item - "<i>'.$title.'</i>"?
        <form action="index.php?job=confirm_delete_news&id='.$id.'" method="POST">
         <input type="submit" class="button_plain" value="confirm">
         &nbsp;<input type="button" class="button_plain" name="cancel" value="cancel" onClick="javascript:history.back()">
    ');
break;
    
case "confirm_delete_news" :
$id = $_REQUEST['id']; // get the id again
    mysql_query("DELETE FROM `news` WHERE `id` = '$id'"); //delete the row with the corresponding id from the database
    die('Item successfully deleted!<br>Click <A href="index.php">here</A> to go back to the main page');

break;
}// end switch
?>




What's that you say? you now wish to edit the articles already in the database?
right-o to edit the articles you will need to pull them up and put them in a textbox/area then you'll need a submit button to simply but it

back in the same spot in the database! If you've been reading properly this should be quite easy to do from memory, but this is the

annotated code to do so, obviously you'll need 2 more cases "edit_news" and "confirm_edit_news".

you will also need another link from the main text saying "edit news" which will send the id of the article to the job "edit_news":


while ($search = mysql_fetch_array($searchSql, MYSQL_ASSOC))// if there are some then retrieve the information
    {
        $newsID = $search["id"]; //get the id of the news item
        $newsTitle = $search["newsTitle"]; //retrieve title
        $newsDate = $search["newsDate"]; //retrieve date
        $newsContent = $search["newsContent"]; //retrieve content
        echo($newsTitle.' - '.$newsDate.'<br>'.$newsContent.'<br><A href="index.php?job=delete_news&id='.$newsID.'">[Delete

News]</A> <A href="index.php?job=edit_news&id='.$newsID.'">[Edit News]</A><br>'); //display the results clearly
    }



now the case for "edit_news":


case "edit_news" :
$id = $_REQUEST['id'];
$searchSql = mysql_query("SELECT * FROM `news` WHERE `id` = '$id'");
$search = mysql_fetch_array($searchSql, MYSQL_ASSOC);
//pull the information from the database
$title = $search["newsTitle"];
$date = $search["newsDate"];
$content = $search["newsContent"];

//this is a form which will display the text currently in the database
//I have chosen for "date" not to be editable you can change this if you wish    
echo('
<form action="index.php?job=confirm_edit_news&id='.$id.'" method="POST">
Title: <input type="text" name="title" value="'.$title.'">
Date: '.$date.'
Content: <TEXTAREA name="content" ROWS=10 COLS=70>'.$content.'</TEXTAREA>
<input type="submit" value="submit" align="left">
');

break;



when submitted that form will send all the new information to a new case "confirm_edit_news" the code for which is:


case "confirm_modify_news" :
    $id = $_REQUEST['id']; //get the id from the previous form
    $title = $_POST['title']; //get the title from the previous form
    $content = $_POST['content']; //get the content from the previous form
    $content = nl2br($content); //convert blank rows into <br>
    
    mysql_query("UPDATE news SET `title` = '$title' WHERE `id` = '$id'");
    mysql_query("UPDATE news SET `content` = '$content' WHERE `id` = '$id'");

    die('Item successfully updated!<br>Click <A href="index.php">here</A> to go back to the main page');
break;



VOILAAA!!! completed, you will now be able to edit the data in the database!!

One last change on the main part we will now need to "ORDER BY `id` DESC" in the SQL statement

here's the final code!!


<?php
/*
Tutorial Copyright 2006 - 2007
Matt Davenport - www.theflashplace.com
*/

include("connection.php"); // connect to the database
$job = $_REQUEST['job']; // this is now needed because there will be many different options for the data

if(!$job){ //if variable job isn't set
//this is an sql statement that will go through the database and select the title, date and content of the news items.
$searchSql = mysql_query("SELECT newsTitle,newsDate,newsContent FROM `news` ORDER BY `id` DESC");

$newsItemsInDatabase = mysql_num_rows($searchSql); //check to see how many rows there are

if($newsItemsInDatabase == 0){ // if there are 0 then:
    echo('Sorry, there are currently no news items!'); // display this message
}else{
while ($search = mysql_fetch_array($searchSql, MYSQL_ASSOC))// if there are some then retrieve the information
    {
        $newsID = $search["id"]; //get the id of the news item
        $newsTitle = $search["newsTitle"]; //retrieve title
        $newsDate = $search["newsDate"]; //retrieve date
        $newsContent = $search["newsContent"]; //retrieve content
        echo($newsTitle.' - '.$newsDate.'<br>'.$newsContent.'<br><A href="index.php?job=delete_news&id='.$newsID.'">[Delete

News]</A> <A href="index.php?job=edit_news&id='.$newsID.'">[Edit News]</A><br>'); //display the results clearly
    }

};
}

switch ($job) //this saves us from writing thousands of if statements!
{
case "add_news" :
    // the next part just places the basic form onto the page
    echo('
    <form name="add_news" method="post" action="index.php?job=confirm_add_news">
    Title: <input type="text" name="title">    <br>
    Content: <TEXTAREA name="content" ROWS=10 COLS=70></TEXTAREA> <br>
    </form>');
break; //exit this case statement

case "confirm_add_news" :
    $date = date("F dS, Y h:i A"); // get todays date using phps date(); function
    $title = $_POST['title']; // get the title from the form
    $content = $_POST['content']; // get the content from the form
    $content = nl2br($content); // convert any space between lines into <br>
    
    // then insert this information into the mySQl database
    mysql_query("INSERT INTO news (newsTitle, newsDate, newsContent) VALUES ('$title', '$date', '$content')") or die(mysql_error());
    
    //users will then see this and can then go back to the main page
    die('Thank-you for submitting this news item, click <A href="index.php">here</A> to go back');
break;

case "delete_news" :
    $id = $_REQUEST['id']; //get the ID of the news article we're deleting
    $deletingSql = mysql_query("SELECT newsTitle FROM `news` WHERE id = $id"); //get the title of this news
    $deleting = mysql_fetch_array($deletingSql, MYSQL_ASSOC);
    $title = $deleting["title"]; //get the title of the to-be-deleted news article

    //now echo a simple for with "confirm" or "cancel"
    // if confirm is hit it will be sent to the case "confirm_delete_news"
    echo('Are you sure you want to delete News item - "<i>'.$title.'</i>"?
        <form action="index.php?job=confirm_delete_news&id='.$id.'" method="POST">
         <input type="submit" class="button_plain" value="confirm">
         &nbsp;<input type="button" class="button_plain" name="cancel" value="cancel" onClick="javascript:history.back()">
    ');
break;
    
case "confirm_delete_news" :
$id = $_REQUEST['id']; // get the id again
    mysql_query("DELETE FROM `news` WHERE `id` = '$id'"); //delete the row with the corresponding id from the database
    die('Item successfully deleted!<br>Click <A href="index.php">here</A> to go back to the main page');

break;

case "edit_news" :
$id = $_REQUEST['id'];
$searchSql = mysql_query("SELECT * FROM `news` WHERE `id` = '$id'");
$search = mysql_fetch_array($searchSql, MYSQL_ASSOC);
//pull the information from the database
$title = $search["newsTitle"];
$date = $search["newsDate"];
$content = $search["newsContent"];

//this is a form which will display the text currently in the database
//I have chosen for "date" not to be editable you can change this if you wish    
echo('
<form action="index.php?job=confirm_edit_news&id='.$id.'" method="POST">
Title: <input type="text" name="title" value="'.$title.'">
Date: '.$date.'
Content: <TEXTAREA name="content" ROWS=10 COLS=70>'.$content.'</TEXTAREA>
<input type="submit" value="submit" align="left">
');

break;

case "confirm_modify_news" :
    $id = $_REQUEST['id']; //get the id from the previous form
    $title = $_POST['title']; //get the title from the previous form
    $content = $_POST['content']; //get the content from the previous form
    $content = nl2br($content); //convert blank rows into <br>
    
    mysql_query("UPDATE news SET `title` = '$title' WHERE `id` = '$id'");
    mysql_query("UPDATE news SET `content` = '$content' WHERE `id` = '$id'");

    die('Item successfully updated!<br>Click <A href="index.php">here</A> to go back to the main page');
break;

}// end switch
?>



That's everything! any problems, post on the forum or comment this :)




Back to tutorials

Sponsored Link



No comments have been left on this tutorial