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 Updating and Deleting Records Updating Records

Backend Intern
Backend Intern
7,148 Points

Database was not updated

I can't seem to update the database on this challenge. Would you be nice and look at my code, maybe i have a syntax error.

index.php
<?php
function reassign_task($old, $new) {
    include 'connection.php';
  $id = 1;
  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  try{
  $query = $db->prepare("UPDATE tasks SET project_id = ? WHERE project_id = ?");
    $query->bindparam(1, $query, PDO::PARAM_INT);
    $query->bindparam(2, $query, PDO::PARAM_INT);

  }catch(Exception $e){
     echo "Message: " . $e->getMessage();
  }
  return $query->execute(array($old,$new));

}
echo reassign_task(1, 1);

1 Answer

You just have a couple tiny issues that can be fixed pretty easily. The first is your casing is wrong for bindparam. It should be bindParam. Secondly, inside your bindParam() you're not actually setting $old and $new. By changing those two lines to this:

$query->bindParam(1, $new, PDO::PARAM_INT);
$query->bindParam(2, $old, PDO::PARAM_INT);

you will establish the binding you need. I think the confusion here is in the way you're trying to use your execute call, which is your final issue. While it can be used similarly (see examples #2 and #3 here though the whole page should help a bit), because you're using bindParam to set up the prepared statement you should call execute() without any parameters.

If you clean up those 3 issues, your code will complete the Challenge as expected and you'll be on your way, :)