Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Why isnt my page being redirected with the header function?

Hey, I'm trying to get my page to redirect if the user successfully adds something to a database. I'm using the header function to achieve this. However, its not working, even if the user was successful logging information to the database.

in my form, the action attr is given a page to go to. But I have a try catch block that attempts to redirect the user upon success. However, the action attr supersedes that header function to page I don't want directed to.

any ideas why the header function isnt working?

<?php
require_once __DIR__ . '/inc/head.php';
require_once __DIR__ . '/inc/nav.php';
?>
<div class="container">
    <div class="well">
        <h2>Add a Music Record</h2>
        <form class="form-horizontal" method="post" action="procedures/addMusic.php">
          <div class="form-group">
    <label for="title" class="col-sm-2 control-label">Title</label>
    <div class="col-sm-10">
        <input type="text" class="form-control" id="title" name="title" placeholder="Book Title" value="">
    </div>
</div>
<div class="form-group">
    <label for="description" class="col-sm-2 control-label">Description</label>
    <div class="col-sm-10">
        <textarea name="description" class="form-control" rows="5" placeholder="Description of the book"></textarea>
    </div>
</div>
<div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-default">Add Book</button>
    </div>
</div>
        </form>
    </div>
</div>
<?php
require_once __DIR__ . '/inc/footer.php';
<?php
function addBook($title, $description) {
    global $db;
    $ownerId = 0;

    try {
        $query = 'INSERT INTO books (name, description, owner_id) VALUES(:name, :description, :ownerId)';
        $stmt = $db->prepare($query);
        $stmt->bindParam(':name', $title);
        $stmt->bindParam(':description', $description);
        $stmt->bindParam(':ownerId', $ownerId);
        return $stmt->execute();
    } catch (\Exception $e) {
        throw $e;
    }
}
?>


<?php
require_once __DIR__ . '/../inc/bootstrap.php';




$bookTitle = $_POST['title'];
$bookDescription = $_POST['description'];


try {

    $newBook = addBook($bookTitle, $bookDescription);

    header('Location: books.php');

} catch (Exception $e){
    header('Location: add.php');
}

The information the user provides is being stored in the database. its just the redirect that isnt working