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

Amber Stevens
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Amber Stevens
Treehouse Project Reviewer

my function to insert into database not working?

I'm trying to make a page that can add a journal entry into my database.
This is from the page with the form and where I call the function:

<?php

if($_SERVER["REQUEST_METHOD"] == 'POST') {
$title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING);
$date = filter_input(INPUT_POST, 'date', FILTER_SANITIZE_STRING);
$time_spent = filter_input(INPUT_POST, 'time_spent', FILTER_SANITIZE_STRING);
$entry = filter_input(INPUT_POST, 'entry', FILTER_SANITIZE_STRING);
$link_name = filter_input(INPUT_POST, 'link_name', FILTER_SANITIZE_STRING);
$link_address = filter_input(INPUT_POST, 'link_address', FILTER_SANITIZE_URL);

new_entry($title, $date, $time_spent, $entry, $link_name, $link_address);

}

and this is the actual function:

function new_entry() {
        include('connection.php');
    $newEntry1 = "INSERT INTO journal(title, date, time_spent, entry)
                  VALUES(?, ?, ?, ?)" ;
    $newEntry2 = "INSERT INTO resources(link_name, link_address) VALUES (?, ?)";
  try {
        $results = $db->prepare($newEntry1);
        $results->bindValue(1, $title, PDO::PARAM_STR);
        $results->bindValue(2, $date, PDO::PARAM_STR);
        $results->bindValue(3, $time_spent, PDO::PARAM_STR);
        $results->bindValue(4, $entry, PDO::PARAM_STR);
        $results->execute();
      }   catch (Exception $e) {
            echo "Unable to add entry <br />" . $e->getMessage();
            return false;

    }
    try {
      $results2 = $db->prepare($newEntry2);
      $results2->bindValue(1, $link_name, PDO::PARAM_STR);
      $results2->bindValue(2, $link_address, PDO::PARAM_STR);
      $results2->execute();
    }  catch (Exception $e)  {
          echo "Unable to add entry <br />" . $e->getMessage();
          return false;

  }
        return true;

}

When I enter all the form data it redirects to the right page but doesn't add anything. What am I doing wrong or missing? Any help would be appreciated...

Amber Stevens
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Amber Stevens
Treehouse Project Reviewer

Dave, thanks for cluing me in to the parameters thing. That was part of the problem. I also changed the action page in my form to the page that the form is on and for some reason that worked.

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Shouldn't the function new_entry() define the parameters?

Are you getting any error messages?