简体   繁体   中英

PHP/MySQL: In a Voting System, how can I keep a user from seeing a profile he has already voted on?

Generally speaking, how can I keep a user from seeing a profile he has already voted on so that he does not vote on it twice?

I have a table (called "Users") of users each with a unique user ID (with column "userID"), I have another table (called "Votes") for each vote given by each user to another user (using their respective user ID's with columns "fromUser", "toUser", "vote"), so if I want to show a user a new random profile to vote on, then what is the BEST way to exclude any users where the current User has already voted on (in other words, exclude any "userID" where the "userID" is the "toUser" and where the "fromUser" is the current user's userID).

Thanks!

SELECT userID FROM users
WHERE userID NOT IN (SELECT toUser FROM votes WHERE fromUser = 'theUserIdThatIsVotingNow')
ORDER BY RAND() LIMIT 1

This should do the trick.

SELECT * 
FROM Users 
WHERE userID NOT IN (SELECT toUser FROM Votes WHERE fromUser = '$current_user_userID');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM