Search code examples
phpmysqlsocial-media-like

disabling more than one "like" per post


I am developing a question posting web application in PHP.

When you log in, you can click on a specific question, and then a new page is opened and there is an option to like that specific question. Every "like" increments field rating in a table "questions"

Now what I want to do is to enable only one like per user. HOW IS IT DONE? :/

These are my tables.

QUESTIONS:

qID int(3)           
qTitle  varchar(200)             
userID  int(2)          users -> userID (foreign key)    
qBody   text                 
rating  int(2)

USERS

userID  int(2)  (PRIMARY KEY)        
username    varchar(40)          
fname   varchar(40)          
lname   varchar(40)          
password    varchar(40)          
email   varchar(50)

Solution

  • Use a third table:

    LIKES

    userID  int(2)
    qID int(2)
    

    When a user 'like's a question, add a row to that table. Before allowing a 'like', first check there is not a matching row in that table.