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 CRUD Operations with PHP Creating Records Accepting User Data

marcell gibbs
marcell gibbs
3,744 Points

cannot add project

when i run this code i get the error message cant seem to figure out why:

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

$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";
    }
  }
}


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 Project</h1>


          <?php 
            if(isset($error_message)) {
              echo "<p class='message'>$error_message</p>";
            }
            ?>
            <form class="form-container form-add" method="post" action="project.php">
                <table>
                    <tr>
                        <th><label for="title">Title<span class="required">*</span></label></th>
                        <td><input type="text" id="title" name="title" value="" /></td>
                    </tr>
                    <tr>
                        <th><label for="category">Category<span class="required">*</span></label></th>
                        <td><select id="category" name="category">
                                <option value="">Select One</option>
                                <option value="Billable">Billable</option>
                                <option value="Charity">Charity</option>
                                <option value="Personal">Personal</option>
                        </select></td>
                    </tr>
                </table>
                <input class="button button--primary button--topic-php" type="submit" value="Submit" />
            </form>
        </div>
    </div>
</div>

<?php include "inc/footer.php"; ?>
Sean T. Unwin
Sean T. Unwin
28,690 Points

This looks okay. If you're getting the "could not add project" error message then the issue may be with add_project() within functions.php.

Feel free to post the code of that file so we can see what you have going on there.

P.S. for others - this question refers to the video, https://teamtreehouse.com/library/-adding-projects, which is after the one in the 'Watch Video' link.

1 Answer

marcell gibbs
marcell gibbs
3,744 Points

Hey thanks you were right I had an issue with where i put return true; at the end of the code