Create Facebook Like System with Jquery MySQL PDO In PHP
Want to get a Facebook Like system in your site ? Youre at the right place. This technic is used in my social network, Friendshood. See the demo there in the social network. But you have to signup.


Create a post table to insert posts created by users.
The id field is an auto increment field which decides the post id. User field is the users id and likes field contains number of users who liked the post. Posted field contains the date and time in which the post was created.
First of all create a config file that will connect to database. We are going to use PDO.
DEMO
You should create a user table with users data including user id, username, password, email etc...
The table should be like this:

Lets start by creating a likes table.

CREATE TABLE `fdlikes` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`pid` int NOT NULL PRIMARY KEY ,
`user` varchar(25) NOT NULL UNIQUE
);
The pid field is for the post id. The user field is for the user id.
Create a post table to insert posts created by users.
CREATE TABLE `fdposts` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`user` varchar(25) NOT NULL,
`message` varchar(200) NOT NULL,
`likes` int(11) DEFAULT NULL,
`posted` varchar(200) NOT NULL,
);

First of all create a config file that will connect to database. We are going to use PDO.
config.php
$dbh = new PDO(mysql:dbname=database;host=127.0.0.1, username, password);
Showing Like/Unlike Button
<?
include("config.php");
$pid=1; //Post id - Post is "Hi everybody"
$uid=1; //User id - User is "Subin Siby"
$sql=$dbh->query("SELECT * FROM fdlikes WHERE pid=$pid and user=$uid");
if($sql->rowCount()==1){
echo <a href="#" class="like" id=".$pid." title="Unlike">Unlike</a>;
else{
echo <a href="#" class="like" id=".$pid." title="Like">Like</a>;
}
?>
jQuery code to post like/unlike action
$(".like").on(click, function(){
if($(this).attr(title)==Like){
$.post(action.php,{pid:$(this).attr(id),action:like},function(){
$(this).text(Unlike);
$(this).attr(title,Unlike);
});
}else{
if($(this).attr(title)==Unlike){
$.post(action.php,{pid:$(this).attr(id),action:unlike},function(){
$(this).text(Like);
$(this).attr(title,Like);
});
}
}
});
PHP code to like/unlike in which jQuery sends request
This file is action.php<?Thats it. Try it out. If theres any bugs, problems or questions feel free to comment below on DisQus.
include("config.php");
$pid=$_POST[pid];
$user=$_COOKIE[user];
$action=$_POST[action];
if ($action==like){
$sql=$dbh->prepare("SELECT * FROM fdlikes WHERE pid=? and user=?");
$sql->execute(array($pid,$user));
$matches=$sql->rowCount();
if($matches==0){
$sql=$dbh->prepare("INSERT INTO fdlikes (pid, user) VALUES(?, ?)");
$sql->execute(array($pid,$user));
$sql=$dbh->prepare("UPDATE fdposts SET likes=likes+1 WHERE id=?");
$sql->execute(array($pid));
}else{
die("There is No Post With That ID");
}
}
if($action==unlike){
$sql=$dbh->prepare("SELECT * FROM fdlikes WHERE pid=? and user=?");
$sql->execute(array($pid,$user));
$matches=$sql->rowCount();
if ($matches!=0){
$sql=$dbh->prepare("DELETE FROM fdlikes WHERE pid=? AND user=?");
$sql->execute(array($pid,$user));
$sql=$dbh->prepare("UPDATE fdposts SET likes=likes-1 WHERE id=?");
$sql->execute(array($pid));
}
}
?>
alternative link download