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

The error message did not display in the browser

i have no idea why the error message is not displaying heres my get_project_list function:

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();
  }                   
}

my add_project() also in the function file:

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

my foreach loop in the project_list file:

<?php
            foreach(get_project_list() as $item) {
             echo "<li>" . $item['title'] . "</li>";
                  }
      ?>

and my conditional in the project file:

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';
   } 
  }
}

edited: Added markdown -jn

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, David Pinheiro! I don't see anything that stands out as obviously incorrect. Hope you don't mind, but I added some markdown so that your code is more easily readable. I'd be interested to know if you could create a snapshot by clicking the camera icon on the upper right-hand side of your workspace and linking it here so I can "fork" it and have a look around. I'm also curious if you're looking at the "Add Project" form, because that should be what is generating the error. Listing the projects should only create an error if it couldn't (for some odd reason) connect to the database.

Let me know! :sparkles:

1 Answer

hey! i just figured it out. turns out i was just misspelling a variable! thanks!! :)))))