-3

Hey I want to create like system in php But I am facing some problem on it ...

How can I create Like system that allow only one like per one user??

This is my code

<?php
if(isset($_POST['like'])){
    $q = "SELECT * FROM likes WHERE `username` = '".$_SESSION['recieveruser']."'";
    $r = mysqli_query($con, $q);
    $count  = mysqli_num_rows($r);
    if ($count == "0") {
        $q1    = "INSERT INTO likes (`username`, `likecount`)VALUES('".$_SESSION['recieveruser']."', '1')";
        $result1 = mysqli_query($con, $q1);
    } else {
        while($row = mysqli_fetch_array($r)) {
            $liked = $row['likecount'];
        }
        $likeus = ++$liked;
        $q2    = "UPDATE likes SET likecount='".$likeus."' WHERE username = '".$_SESSION['recieveruser']."'";
        $result2 = mysqli_query($con, $q2);
    }
}

give me some suggestions

  • I want only one like per user

In this code every user can give Many likes to another user but I want only one like per one user and I want to display the name of the user who gave like if it's possible

This is only user like code...

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 4
    [Little Bobby](http://bobby-tables.com/) says [you may be at risk for SQL Injection Attacks](https://stackoverflow.com/q/60174/). Learn about [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement) with [parameterized queries](https://stackoverflow.com/a/4712113/5827005). – GrumpyCrouton Jul 10 '20 at 11:46

2 Answers2

0

I created simliar like system on my website. In my likes table, I had these columns:

  • Id of comment, that has been liked
  • Id of user who liked
  • Id of like (for removal)

When user clicked like, I inserted new row into likes table, with two known values. ID of like was autoincremented.

To show number of likes, I filtered by id of comment and grouped by users id (just to be sure). The number was obtained using count.

select count(*) from likes where comment_id = 666 group by user_id;

Even if you let user insert multiple times, the like counts only as one. But best would be to check, if current user already liked and dont let him do that. For this task, insert on duplicate key update could be used, to spare if exists db request (select).

Kudlas
  • 659
  • 7
  • 24
0

You should not use the code you posted above. First of all, your code is vulnerable to SQL-Injections and therefore you should use Prepared Statements (https://www.php.net/manual/de/mysqli.quickstart.prepared-statements.php). Second, $_SESSION variables are depricated (https://www.php.net/manual/en/reserved.variables.session.php).

Lets assume you want users only to be able to like a post once. Then, instead of the column likecount you would need a post-id which uniquely identifies the post. Define the combination post-id and username as a primary key in your database.

Now your code just have to check whether you find the username with the according post-id in the table likes.

In case you do not find the username with the according post-id in the table, you have to INSERT the username and the post-id

SevenOfNine
  • 630
  • 1
  • 6
  • 25