Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do i create Facebook like, like system using this code for Register users

Tags:

php

Please Help Me For This Code I Have Already Asked Help For This Code But I Couldn't Get Help From Any One So My Problem is I Want Likes and Unlikes for Every Register user and also want to check that no same(login user) user should like multiple times for that register user

I have table userwhich has user Data and another table likes which has fields 1.userID 2.status_id 3.like 4.un-like

I have also Confusion in My WHERE Clause and Stored $userID Varaiable Please help For That also

<?php
session_start();
include('../config.php');
$userID = intval($_REQUEST['userID']);

$sql = "SELECT * FROM likes WHERE userID = '".$userID."'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res);

$like       = $row['like'];
$unlike     = $row['un-like'];
if($_POST)
{   
    if(mysql_real_escape_string($_POST['op']) == 'like')
    {
        $update = "`like`=`like`+1";
    }
    if(mysql_real_escape_string($_POST['op']) == 'un-like')
    {
        $update = "`un-like`=`un-like`+1";
    }

    mysql_query("UPDATE likes SET $update WHERE userID ='".$userID."'");
    echo 1;
    exit;   
}
?>
<div class="grid">
<span id="status"></span><br>
<input type="button" value="<?php echo $like; ?>" class="button_like" id="likeBtn" />
<input type="button" value="<?php echo $unlike; ?>" class="button_unlike" id="unlikeBtn" />
</div>
like image 546
Abhi Burk Avatar asked Feb 11 '26 11:02

Abhi Burk


1 Answers

You can take different approach to implement this. One way would be to configure UNIQUE KEY Index to your likes table on the fields userID and status_id, and then modify the UPDATE to INSERT ... ON DUPLICATE KEY UPDATE type query. This will let you add a new record if not liked/disliked already and update the fields if the like record was already inserted.

Steps to implement this:

Step 1:

Add a UNIQUE KEY index on the fields userId and status_id

CREATE UNIQUE INDEX userId_statusId ON `likes` (`userId`, `status_id`)

Step 2:

Modify your UPDATE query to something like below

INSERT INTO `likes` (`userId`, `status_id`, `like`, `un-like`) VALUES ($userID, $statusId, 1, 0)
  ON DUPLICATE KEY UPDATE 
    `like`= VALUES(`like`),
    `un-like` = VALUES(`un-like`);

Note that I have added status_id to the query which you are not factoring in your code. You need to pass it in to this page like userId.

Also the like and un-like field is hardcoded in my query. You can make them dynamic in your IF blocks.

More reads:

http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html http://dev.mysql.com/doc/refman/5.7/en/create-index.html

Your final code would look something like this

<?php
session_start();
include('../config.php');
$userID = intval($_REQUEST['userID']);
$statusId = intval($_REQUEST['statusID']);

if($_POST)
{   

    $like = 0;
    $unlike = 0;

    if(mysql_real_escape_string($_POST['op']) == 'like')
    {
        $like = 1;
    }
    if(mysql_real_escape_string($_POST['op']) == 'un-like')
    {
        $unlike = 1;
    }

    mysql_query("INSERT INTO `likes` (`userId`, `status_id`, `likes`, `un-like`) VALUES ($userID, $statusId, $like, $unlike)
                  ON DUPLICATE KEY UPDATE 
                    `likes`= VALUES(`likes`),
                    `un-like` = VALUES(`un-like`);");
    echo 1;
    exit;   
}
?>
like image 191
Deepak Avatar answered Feb 15 '26 19:02

Deepak