7.5. Updating Entries in the Database

In your form, you added a hidden input to store the entry's ID. This hidden input is what you use to determine whether a form submission is an edit or a new entry.

To make this distinction, you need to check whether $_GET['id'] is empty. If so, the entry is new, and you can proceed as usual. If $_GET['id'] has a value, however, you're editing an entry, and you must use a different query.

You update an entry in the entries table by specifying which fields are being set to which value. Your ID won't change, but the title, url, and entry fields all might, so your query needs to look like this:

UPDATE entries
SET title=?, entry=?, url=?
WHERE id=?
LIMIT 1

This query updates a maximum of one entry in the entries table by matching the supplied ID with the submitted title, entry, and url values.

You can check whether $_GET['id'] contains a value and update an entry by inserting the code highlighted in bold in update.inc.php:

<?php

// Include the functions so you can create a URL
include_once 'functions.inc.php';

if($_SERVER['REQUEST_METHOD']=='POST'
    && $_POST['submit']=='Save Entry'
    && !empty($_POST['page'])
    && !empty($_POST['title'])
    && !empty($_POST['entry']))
{
    // Create a URL to save in the database
    $url = makeUrl($_POST['title']);

    // Include database credentials and connect to the database
    include_once 'db.inc.php';
    $db = new PDO(DB_INFO, DB_USER, DB_PASS);

    // Edit an existing entry
    if(!empty($_POST['id']))
    {
        $sql = "UPDATE entries
                SET title=?, entry=?, url=?
                WHERE id=?
                LIMIT 1";
        $stmt = $db->prepare($sql);
        $stmt->execute(
            array(
                $_POST['title'],
                $_POST['entry'],
                $url,
                $_POST['id']
            )
        );
        $stmt->closeCursor();
    }

// Create a new entry
    else
    {
        // Save the entry into the database
        $sql = "INSERT INTO entries (page, title, entry, url)
                VALUES (?, ?, ?, ?)";
        $stmt = $db->prepare($sql);
        $stmt->execute(
            array(
                $_POST['page'],
                $_POST['title'],
                $_POST['entry'],
                $url
            )
        );
        $stmt->closeCursor();
    }

    // Sanitize the page information for use in the success URL
    $page = htmlentities(strip_tags($_POST['page']));

    // Send the user to the new entry
    header('Location: /simple_blog/'.$page.'/'.$url);
    exit;
}

else
{
    header('Location: ../'),
    exit;
}

?>

NOTE

Make sure you add the closing curly brace for the else statement (just after $stmt->closeCursor();) to avoid a parsing error.

Now you can update entries in your database. To test this ability, open your application in a browser and click the edit link on one of your entries and add some new text. Click the Save Entry button to see the edited entry (see Figure 7-3).

Figure 7.3. An edited entry

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

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