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

David Curran
David Curran
7,682 Points

Update Voting System By Alena Holligan

I have a question regarding the Update Voting System.

I'm not getting my head around the Vote function.

Why is the clearVote the first function called to delete the users entry? Is this so that there can only be ever one entry in the database?

Allena says that when users vote is deleted a count greater than 0 will be returned, I am not understanding the reason why a value of greater than 0 would be returned if its been deleted? Whats the significance of the value being greater than 1.

Can anyone explain this function methodology. Thanks a lot.

3 Answers

I had to load the page to see how the voting works. If you already voted for a book clicking either arrow removes the vote. This means a user can only vote once per book and is accomplished with clearVote.

clearVote attempts to delete a vote based on bookid and userid.

Here is the function:

function clearVote($bookId, $userId)
{
    global $db;

    try {
        $query = "DELETE 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 $stmt->rowCount();
    } catch (\Exception $e) {
        throw $e;
    }
}

If you check the return statement for clearVote you will see a rowCount is returned. This is a count of the number of records affected by the delete query. If any records were deleted the value will be positive. This adjustment is considered a vote. So the following returns true and the vote function is exited

if (clearVote($bookId, $userId)) {
        return true;
    }

If no vote records were deleted, then the user hasn't voted yet and the vote function proceeds to add a new vote.

Yes. The user will have no votes in the vote table for that book. With no votes, on a subsequent vote clearVote will return a recordCount of 0 which is equivalent to false. So a new vote will be recorded.

David Curran
David Curran
7,682 Points

Hi KRIS NIKOLAISEN, thank you for your help on this issue. So, if the user has voted, then the clearVote function will run and remove the entry in the votes table and return a positive count, then return true and exit the vote function. Please excuse my confusion but does that mean the user has no longer got a vote for the book in the votes table as it has been deleted?