Click here to Skip to main content
15,887,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am implementing a like and unlike system to my project. I followed this tutorial to make things works. I trying to make an undo like/dislike function if I click again on the same button, but I can't figure out, what should I modify or add to codes.

What I have tried:

I have tried to check if the record is exist in the database then if yes, delete it, but nothing happens:


$query = "SELECT * FROM like_unlike WHERE type='1' and userid = '$userid' and postid='$postid'";
$result = mysqli_query($conn,$query);
$fetchliked = mysqli_fetch_array($result);
$liked = $fetchliked['type'];
if ($liked == '1') {
    $deletequery = "DELETE FROM like_unlike WHERE type='1' userid='$userid' and postid='$postid'";
    mysqli_query($conn,$deletequery);
}
Posted
Updated 24-May-19 9:18am
v5
Comments
Richard Deeming 24-May-19 15:12pm    
Your code is almost certainly vulnerable to SQL Injection[^]. NEVER use string concatenation (or interpolation) to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]

Quote:
$deletequery = "DELETE FROM like_unlike WHERE type='1' where userid='$userid' and postid='$postid'";

You've got two WHERE clauses in your DELETE statement. Unless it's a MySql-special, you should only have one:
$deletequery = "DELETE FROM like_unlike WHERE type='1' and userid='$userid' and postid='$postid'";

But you really need to fix the SQL Injection[^] vulnerabilities. :)
 
Share this answer
 
Comments
Galarist_00 24-May-19 15:19pm    
Yeah I realised that. Even tho I have fixed that. It still not working...
Galarist_00 24-May-19 15:20pm    
Probably I need to change something in the AJAX as well
You're interpreting "postid" differently in the SELECT and DELETE "where" clauses; a literal in the one case (DELETE) and something else in the other.
 
Share this answer
 
Comments
Galarist_00 24-May-19 15:21pm    
yeah fixed that one. Still not working...
[no name] 24-May-19 15:29pm    
Didn't see your "fix" or what was actually required.
Galarist_00 24-May-19 15:35pm    
What do you mean? I fixed that syntax error on the delete query but still not undo the like or unlike if I click again on the same submitted button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900