7.6. Handling Entry Deletion

You've almost completed your administrative controls. All that's left is to add the ability to delete entries. This is fairly straightforward, due to the way that you've constructed the delete link.

The link passes delete as $_GET['page'], so it's easy to identify entries marked for deletion. In admin.php, you check whether $_GET['page'] == 'delete', then pass the entry URL to be deleted to a function called confirmDelete()—you'll write this function in the next section. This function asks the user to confirm that she does in fact wish to delete the entry.

Do this by adding the following code in bold to admin.php:

<?php

    /*
     * Include the necessary files
     */
    include_once 'inc/functions.inc.php';
    include_once 'inc/db.inc.php';

    // Open a database connection
    $db = new PDO(DB_INFO, DB_USER, DB_PASS);

$page = htmlentities(strip_tags($_GET['page']));

    if(isset($_GET['url']))
    {
        $url = htmlentities(strip_tags($_GET['url']));

        // Check if the entry should be deleted
        if($page == 'delete')
        {
            $confirm = confirmDelete($db, $url);
        }

        // Set the legend of the form
        $legend = "Edit This Entry";

        $e = retrieveEntries($db, $page, $url);
        $id = $e['id'];
        $title = $e['title'];
        $entry = $e['entry'];
    }
    else
    {
        // Set the legend
        $legend = "New Entry Submission";

        // Set the variables to null if not editing
        $id = NULL;
        $title = NULL;
        $entry = NULL;
    }

?>

confirmDelete() is called when the user clicks the delete link, and its return value is stored in the $confirm variable. This function returns an HTML form asking the user to confirm that she wishes to delete the entry in question. To display this form, you need to add a conditional statement in the body of admin.php that displays the confirmation form if the value of $page is delete.

Add the code in bold to admin.php to display your confirmation page:

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <link rel="stylesheet" href="/simple_blog/css/default.css" type="text/css" />
    <title> Simple Blog </title>
</head>

<body>
    <h1> Simple Blog Application </h1>

<?php

    if($page == 'delete'):
    {
        echo $confirm;
    }
    else:

?>
    <form method="post"
        action="/simple_blog/inc/update.inc.php"
        enctype="multipart/form-data">
        <fieldset>
            <legend><?php echo $legend ?></legend>
            <label>Title
                <input type="text" name="title" maxlength="150"
                    value="<?php echo $title ?>" />
            </label>
            <label>Image
                <input type="file" name="image" />
            </label>
            <label>Entry
                <textarea name="entry" cols="45"
                    rows="10"><?php echo $entry ?></textarea>
            </label>
            <input type="hidden" name="id"
                value="<?php echo $id ?>" />
            <input type="hidden" name="page"
                value="<?php echo $page ?>" />
            <input type="submit" name="submit" value="Save Entry" />
            <input type="submit" name="submit" value="Cancel" />
        </fieldset>
    </form>
<?php endif; ?>
</body>

</html>

NOTE

Don't forget to close the else statement by inserting <?php endif; ?> just above the closing </body> tag.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.133.180