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 Introduction to User Authentication in PHP Setting Up Authorization Update Voting System

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Missing function getUserVote()

Hi everyone.

I've just about finished with this course and I have been able to follow along. I have one question which I'll get to in a moment. However, in my project files there is one function that is missing that appears in this video which is the getUserVote() function that goes in functions_vote.php. Without this and following the changes in the video this will remove all books from the book list page.

To retrieve the book list you may need to add the following method to functions_vote.php if it is not there already.

<?php 
function getUserVote($bookId, $userId = 0) {
    global $db;

    try {
     $query = "SELECT value FROM votes "
     . " WHERE book_id = :bookId"
     . " AND user_id = :userId";
         $stmt = $db->prepare($query);
         $stmt->bindParam(':bookId', $bookId);
         $stmt->bindParam(':userId', $userId);
         $stmt->execute();
      return (int) $stmt->fetchColumn();
    } catch (\Exception $e) {
        throw $e;
    }

 } ?>

I was able to narrow this down by building up the code sequentially as in my case I didn't get any error message initially. Just a working site with no book list.

My second point is this.

In my implementation of the voting system, I don't actually get a vote score for an individual user. I get either the word up or down where the score should be. Nor do I get the class that changes the colour of the relevant button. Any idea what might be behind this? I can't see where in the code the up and down text comes from.