2

Hey i am using a query like this:

INSERT INTO likes( 
         likes_memory_id, 
         likes_comment_id, 
         likes_owner_id, 
         likes_like
 ) VALUES (
         :likes_memory_id, 
         :likes_comment_id, 
         :likes_owner_id, 
         :likes_like)

when ever an user click the like button, this query adds a new row. so this query allows to like multiple time. to prevent this i may use a select statement and i might succeed in two queries but i assue there is a better way to do it. (I made research about if not exists statement but i didnt understand too much ) How do I avoid multiple likes?

1
  • by checking before record insert.. Like_-First Select record, check if exist or not that do actions. Commented May 31, 2014 at 6:49

4 Answers 4

2

The simplest is to create a unique index on your columns which you want unique;

CREATE UNIQUE INDEX uq_mem_own ON likes(
 likes_memory_id, likes_owner_id
);

...and insert likes using INSERT IGNORE, which will insert the value if it's not prevented by the index, otherwise just ignore it;

INSERT IGNORE INTO likes( 
 likes_memory_id, 
 likes_owner_id, 
 likes_like
 ) VALUES (
 :likes_memory_id, 
 :likes_owner_id, 
 :likes_like)
Sign up to request clarification or add additional context in comments.

Comments

1
INSERT INTO likes( 
     likes_memory_id, 
     likes_comment_id, 
     likes_owner_id, 
     likes_like
SELECT * 
FROM (:likes_memory_id, :likes_comment_id, :likes_owner_id ,:likes_like) tmp
WHERE NOT EXISTS ( SELECT * 
                   FROM `likes` 
                   WHERE `likes_memory_id` =  :likes_memory_id
                     AND `likes_comment_id` = :likes_comment_id
                     AND `likes_owner_id` = :likes_owner_id
                     AND `likes_like` = :likes_like
                 ) LIMIT 1;

3 Comments

i got an error with not exists one "MySQL server version for the right syntax to use near 'WHERE NOT EXISTS ( SELECT * FROM likes WHERE likes_memory_id = ? AND" what is wrong with this?
yes i did. it inserted the row alright, didnt give any error, however it allow duplicate rows.
what are those? table_listnames (name, address, tele)
0
You can accomplish this either disable like button after hit or 
using the following query

     INSERT INTO likes 
             (likes_memory_id, 
              likes_comment_id, 
              likes_owner_id, 
              likes_like)
SELECT t1.likes_owner_id
FROM likes t1
WHERE NOT EXISTS(SELECT likes_owner_id
                FROM likes t2
               WHERE t2.likes_owner_id = t1.likes_owner_id);

and make sure that your likes_owner_id is primary or a unique key 

1 Comment

Well likes_owner_id cannot be primary or an unique key because a person might like more than one comment.
-1

Great question! You can simply collect the IP Address of the user and if this user tries to "like" twice it wont record.

STEP 1: Create a new colum called IP (or whatever you like) [this will be used to collect the user IP Address.)

STEP 2: Set IP to the PRIMARY KEY (This is what prevents duplicate entries)

STEP 3: add it to your query.

INSERT INTO likes( 
         IP,
         likes_memory_id, 
         likes_comment_id, 
         likes_owner_id, 
         likes_like
 ) VALUES (
         :IP,
         :likes_memory_id, 
         :likes_comment_id, 
         :likes_owner_id, 
         :likes_like)

STEP 4: Now all you have to do is add a line of code to collect the user IP and submit that along with the other info.

2 Comments

IP? what if user uses another pc or his/her mobile phone?
There is no effective way of determining if it's the same user if the user uses a different computer (unless you collect an email or phone number from them before processing the "like". If that is the case then you can use the email or phone # as the Primary Key). Other than that, the only thing that uniquely identifies a user is the IP. Hope this helps @suyilmaz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.