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!

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 CRUD Operations with PHP Creating Records Remembering Form Data

marcell gibbs
marcell gibbs
3,744 Points

error message

when i run my code i get the could not add project message cannot figure out why?

<?php require 'inc/functions.php';

$pageTitle = "Task | Time Tracker"; $page = "tasks"; $project_id = $title = $date = $time = "";

if ($_SERVER['REQUEST_METHOD'] == 'POST') { $project_id = trim(filter_input(INPUT_POST, 'project_id', FILTER_SANITIZE_NUMBER_INT)); $title = trim(filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING)); $date = trim(filter_input(INPUT_POST, 'date', FILTER_SANITIZE_STRING)); $time = trim(filter_input(INPUT_POST, 'time', FILTER_SANITIZE_NUMBER_INT));

if(empty($project_id) || empty($title) || empty($date) || empty($time)){ $error_message = "please fill in the required fields : Project, Title, Date, Time"; } else {

if (add_task($project_id, $title, $date, $time)) {
  header('Location: task_list.php');

} else{ $error_message = "could not add task"; }

} }

include 'inc/header.php'; ?>

<div class="section page"> <div class="col-container page-container"> <div class="col col-70-md col-60-lg col-center"> <h1 class="actions-header">Add Task</h1> <?php if(isset($error_message)) { echo "<p class='message'>$error_message</p>"; } ?> <form class="form-container form-add" method="post" action="task.php"> <table> <tr> <th> <label for="project_id">Project</label> </th> <td> <select name="project_id" id="project_id"> <option value="">Select One</option> <?php foreach(get_project_list() as $item){ echo "<option value='" . $item['project_id'] . "'";

                      if($project_id == $item['project_id']) {
                        echo 'selected';
                      echo ">" .  $item['title'] . "</option>";

                    <th><label for="title">Title<span class="required">*</span></label></th>
                    <td><input type="text" id="title" name="title" value="<?php echo htmlspecialchars($title);?>" /></td>
                    <th><label for="date">Date<span class="required">*</span></label></th>
                    <td><input type="text" id="date" name="date" value="<?php echo htmlspecialchars($date);?>" placeholder="mm/dd/yyyy" /></td>
                    <th><label for="time">Time<span class="required">*</span></label></th>
                    <td><input type="text" id="time" name="time" value="<?php echo htmlspecialchars($time);?>" /> minutes</td>
            <input class="button button--primary button--topic-php" type="submit" value="Submit" />


<?php include "inc/footer.php"; ?>


<?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 get_task_list() { include"connection.php";

$sql = 'SELECT tasks. *, projects.title as project FROM tasks' .' JOIN projects ON tasks.project_id = projects.project_id';

try{ return $db->query($sql); return $db->query($sql); } 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, $category, PDO::PARAM_STR); $results->execute();

} catch (Exception $e) { echo "Error!: " . $e->getMessage() . "<br />"; return false;

} return true; }

function add_task($project_id,$title, $date, $time){ include('connection.php');

$sql = 'INSERT INTO tasks(project_id, title, date, time) VALUES(?,?.?,?)';

try{ $results = $db->prepare($sql); $results->bindvalue(1, $project_id, PDO::PARAM_INT); $results->bindValue(2, $title, PDO::PARAM_STR); $results->bindvalue(1, $date, PDO::PARAM_STR); $results->bindValue(2, $time, PDO::PARAM_INT); $results->execute();

} catch (Exception $e) { echo "Error!: " . $e->getMessage() . "<br />"; return false;

} return true; }