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

The task gets added but I'm not redirected to the tasks. I just get an error saying, "Could not add project".

Here's the code....

<?php
//application functions

function get_project_list() {
    include 'connection.php';

    try {
        return $db->query( 'SELECT project_id, title, category FROM projects' );
    } catch(Exception $e) {
        echo "Error!: " . $e->getMessage() . "<br />";
        return array();
    }
}


function add_project($title, $category){
    include 'connection.php';

    $sql = 'INSERT INTO projects(title, category) VALUES(?, ?)';

    try {
        $results = $db->prepare($sql);
        $results->bindValue(1, $title, PDO::PARAM_STR);
        $results->bindValue(2, $title, PDO::PARAM_STR);
        $results->execute();
    } catch (Exception $e) {
        echo "Error!: " . $e->getMessage() . "<br />";
        return false;
    }
}
$pageTitle = "Project | Time Tracker";
$page = "projects";

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $title = trim(filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING));
    $category = trim(filter_input(INPUT_POST, 'category', FILTER_SANITIZE_STRING));

    if (empty($title) || empty($category)) {
        $error_message = 'Please fill in the required fields: Title, Category';
    } else {
        if (add_project($title, $category)) {
            header('Location: project_list.php');
            exit;
        } else {
            $error_message = 'Could not add project';
        }
    }
}```