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

I stuck on PHP User Authentication Challenge Task 1 of 1 pls help me to solve the problem

We have two separate functions for promoting and demoting a user. This code for each of these functions is almost exactly the same. We are going to refactor our code so that there is only a single function that change change the user role. Create a new function named "changeRole" that accepts 2 parameters. The first should be the $userId and the second should be the $roleId. Update the database by binding these two parameters to a SQL statement the updates the users table with the new role of the passed id. Remove the "promote" and "demote" functions.

This is my code.. <?php require DIR . '/inc/bootstrap.php';

function changeRole($userId, $roleId) { global $db;

try { $query = "UPDATE users SET nwewRole=:newRole WHERE id = :passedId'"; $stmt = $db->prepare(); $stmt->bindParam(1,$passedId); $stmt->execute(); } catch (\Exception $e) { throw $e; } }

1 Answer

Ivan Penchev
Ivan Penchev
13,833 Points

Well no one answered, so let me. your answer only binds one parameter and not the two.

require __DIR__ . '/inc/bootstrap.php';

function changeRole($userId, $roleId) {
   global $db;

   try {
       $query = "UPDATE users SET role_id=? WHERE id = ?";
       $stmt = $db->prepare($query);
       $stmt->bindParam(1, $roleId);
       $stmt->bindParam(2, $userId);
       $stmt->execute();
   } catch (\Exception $e) {
       throw $e;
   }
}