7.4. Populating Your Form with the Entry to Be Edited

admin.php is receiving entry URLs when a user clicks the edit link is clicked; next you need to write a script that identifies that URL and loads the appropriate entry. You also need to add the entry's values to the administrative form to enable editing.

Your first step is to check whether $_GET['url'] is set, which determines whether you're editing an entry or creating a new one. If an entry is being edited, you need to load the existing entry data and save each piece in a variable. Fortunately, you've already written the function to load an entry using the URL—retreiveEntries()—so you can use that to load the entry to be edited.

To use retrieveEntries() in your script, you must include the necessary files and open a database connection.

You want to avoid the possibility of having undefined variables, so you should also add an else to your conditional that will declare your entry data variables as NULL if no entry is passed.

Also, you can enhance your form's friendliness by changing the legend to indicate whether you're editing an existing entry or creating a new one. You can store this information in a variable ($legend).

To do this, open admin.php and add the lines of code in bold:

<?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 = isset($_GET['page']) ? htmlentities(strip_tags($_GET['page'])) : 'blog';

    if(isset($_GET['url']))
    {
        // Do basic sanitization of the url variable
        $url = htmlentities(strip_tags($_GET['url']));

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

        // Load the entry to be edited
        $e = retrieveEntries($db, $page, $url);

        // Save each entry field as individual variables
        $id = $e['id'];
        $title = $e['title'];
        $entry = $e['entry'];
    }

else
    {
        // Set the legend
        $legend = "New Entry Submission";

        // Set variables to NULL if not editing
        $id = NULL;
        $title = NULL;
        $entry = NULL;
    }
?>

To add these values into your form, you need to set the value attribute in your inputs and place the $entry variable between the opening and closing <textarea> tags. Also, you need to add a new hidden input named id to contain the entry ID, which will help you in your next step, when you save your changes.

You can add the values into your form by modifying admin.php with the lines of code in bold, as shown:

<form method="post" action="/simple_blog/inc/update.inc.php">
        <fieldset>
            <legend><?php echo $legend ?></legend>
            <label>Title
                <input type="text" name="title" maxlength="150"
                    value="<?php echo htmlentities($title) ?>" />
            </label>
            <label>Entry
                <textarea name="entry" cols="45"
                    rows="10"><?php echo sanitizeData($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>

Clicking the edit link on one of your entries now loads and displays the contents of that entry into the form (see Figure 7-2).

Figure 7.2. Clicking a link loads that entry into the form for editing

Next you need to modify update.inc.php so it recognizes that an entry is being edited and updates the proper entry, as opposed to creating a new entry in the database.

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

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