Build VOTE / LIKE, UNLIKE Feature: PHP & MySQL


In Voting system we need to make sure, one person votes only once, to a particular poll. Or One person can only like a particular image once.
Person who votes must be allowed to vote down and the person who likes must be allowed to unlike the same image / item etc.

Looks complicated? Relax, its not that complicated!

Let me explain the logic and direct you to some videos..
First, build your User Signup and Login forms.
Make username as primary key.

Now create another table with two fields. id(primary key and auto_increment) and image.

Now another table with two fields again: username and id
Make username and id as primary keys..that is, composite primary key.

In database design, a composite key is a key that consists of 2 or more attributes that uniquely identify an entity occurrence. Each attribute that makes up the compound key is a simple key in its own right.

This way, same user can not like / vote for the same image twice! It would throw error, as it violets composite key rule. You could make use of Die() and show appropriate message or still better use if else and if the query has not executed(due to violation of composite key rule) show links in the else part to vote down or unlike. There you could simply delete the entry from vote table.

Video Tutorial: Build VOTE / LIKE, UNLIKE Feature: PHP & MySQL


[youtube https://www.youtube.com/watch?v=XZ6hBLNayr0]

YouTube Link: https://www.youtube.com/watch?v=XZ6hBLNayr0 [Watch the Video In Full Screen.]



Take 3 tables: user, image and vote
username is primary key in user table.
id is primary key in image table. attributes: id and image
id and username are composite primary key in vote table.

Vote / Like, Unlike

1
mysql> INSERT INTO vote VALUES( ‘2’, ’apple’ );

Count Vote / Like

1
mysql> SELECT count(*) FROM vote WHERE id= ‘2’;

Vote down / Unlike

1
mysql> DELETE FROM vote WHERE( id=‘2’ AND username=’apple’ );

Make sure to take the values of id and username dynamically. Am showing 2 and apple for the sake of illustration.
username will be present inside the session variable once the user logs in.
id value will be associated with the like or vote link/button of the image. [id is unique to each individual image]

Taking advantage of the error thrown by the composite primary key. i.e., using if else we check if the user has already voted or liked particular image or participated in a particular poll, if yes, we show him/her unlike or vote down link/button.

Vote, Vote Down or Like, Unlike Feature

1
2
3
4
If( mysql_query(“INSERT INTO vote VALUES( ‘2’, ’apple’ )”)  )
  echo “Thanks for the vote”;
else
  echo “<a href=‘vote.php?down=1’>Vote Down</a>”;

You could mix this simple logic with some CSS and other stuffs like AJAX and implement the voting system easily in a short period of time, like a pro!

3 thoughts on “Build VOTE / LIKE, UNLIKE Feature: PHP & MySQL”

  1. Thanks very much.I really appreciate your kind gesture.Please i need more topics on databases design

Leave a Reply

Your email address will not be published. Required fields are marked *