disabling more than one "like" per post

120 views Asked by At

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)
2

There are 2 answers

0
Joe Daley On BEST ANSWER

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.

0
Martin Smith On

You would need a table recording userID and qID with a unique constraint on the 2 columns (via a composite primary key).

You could still keep the rating column and increment after successful insert to this table (perhaps via a trigger) as a denormalised field for performance reasons.