In this tutorial we’re going to make Facebook kind of like button. To engage users, like button is another important thing to think about. There are so many ways in which one can make a like button.

php like button system

These are the require basic tables to make this like button in the tutorial are:

  • Users table

  • Article table

  • Like table
Many also reading this tutorial: How to Get Value from URL Using PHP Programming Language

But I am not going to make all these tables, I’m going to skip users-table and article-table.

Note: We’re going to use the getting value from url/link method. So if you have not read through how to get a value from url then click here to read.

So now I hope you know all about getting value from link. The next thing is to make your like-table. The like_table carry the following rows, like_id, user_id and post_id.

These are the two files we’re going to create in this tutorial:

  • index.php
  • like.php

So now let’s begin coding the index.php page:

<!DOCTYPE html>
<title>php like button system</title>
$mydatabase_connection = new mysqli(“localhost”,”root”,””,”like_database”);

$post_id = 10;

$find = “SELECT * FROM like_table WHERE post_id=’{$post_id}’”;

$result = $mydatabase_connection->query($find);
$count = mysqli_num_rows($result);

echo $count;
echo “<a href=’like.php?like={$post_id}’>like</a>”;

This is the simplest way of making like system. We’re going to restrict single user from liking per article more than one time. So a user have only one chance of liking one post/article.

Now let’s move on to the like.php file. In this page I’ll assign value into a variable and making it the user-id because we’re not going to make login system and session starting query.

Note: In the like table we’ve three rows, like_id (AUTO_INCREMENT), post_id (id of the post to be like) and user_id (the id of the user who like the post).

Like.php file:

 $post_id = (int)$_GET["like"];

$user_id = 1;
$find = “SELECT * FROM like_table WHERE user_id=’{$user_id}’ AND post_id = ‘{$post_id}’”;

 $result = $mydb->query($find);
 if(!$row = $result->fetch_assoc())
 $insert = “INSERT INTO like_table SET user_id = `{$user_id}`, post_id = `$post_id`”;

 $out_come = $mydb->query($insert);

 header("Location: index.php");

So that’s all. This is how you can make php like button system. This is just basic tutorial, there are a lot more to achieve with this.

Thanks for reading, share with friends!