I am developing a Like and Unlike feature, So that a user can Like/Unlike a product.
I am done with everything after following this tutorial https://www.sourcecodester.com/tutorials/php/11588/easy-and-simple-likeunlike-using-ajaxjquery.html
The problem is that it is not storing anything into the database(like table). And no error was shown. When I click Like, it shows Unlike as it should but thats just it - The likes counter doesn't increase and nothing is stored in database.
index.php
<?php
$bid = $_GET['id'];
$stmt = "SELECT * FROM `like` WHERE postid = :postid AND userid = :userid";
if($querys = $pdo->prepare($stmt)){
$querys->bindValue(':postid', $bid);
$querys->bindValue(':userid', $userid);
$querys->execute();
$check = $querys->rowCount();
}
if($check>0)
{
$like = '<button value="'.$bid.'" class="unlike">Unlike</button>';
}else{
$like = '<button value="'.$bid.'" class="like">Like</button>';
}
$sql = "SELECT count(*) FROM `like` WHERE postid = :postid";
if($query = $pdo->prepare($sql)){
$query->bindValue(':postid', $bid);
$query->execute();
$nlikes = $query->fetchColumn();
//rest of code here
}
echo $like;
<span id="show_like<?php echo $bid; ?>">
<?php echo $nlikes;
?>
javascript/ajax
<script type = "text/javascript">
$(document).ready(function(){
$(document).on('click', '.like', function(){
var id=$(this).val();
var $this = $(this);
$this.toggleClass('like');
if($this.hasClass('like')){
$this.text('Like');
} else {
$this.text('Unlike');
$this.addClass("unlike");
}
$.ajax({
type: "POST",
url: "like.php",
data: {
id: id,
like: 1,
},
success: function(){
showLike(id);
}
});
});
$(document).on('click', '.unlike', function(){
var id=$(this).val();
var $this = $(this);
$this.toggleClass('unlike');
if($this.hasClass('unlike')){
$this.text('Unlike');
} else {
$this.text('Like');
$this.addClass("like");
}
$.ajax({
type: "POST",
url: "like.php",
data: {
id: id,
like: 1,
},
success: function(){
showLike(id);
}
});
});
});
function showLike(id){
$.ajax({
url: 'show_like.php',
type: 'POST',
async: false,
data:{
id: id,
showlike: 1
},
success: function(response){
$('#show_like'+id).html(response);
}
});
}
like.php
<?php
session_start();
ERROR_REPORTING(E_ALL & ~E_NOTICE);
include('php/connect.php');
include('php/function.php');
if (isset($_COOKIE['remember']) && $userid!==null) {
getcookie();
}
if (isset($_POST['like'])){
$id = $_POST['id'];
$sql = "SELECT * FROM `like` WHERE postid = :postid AND userid = :userid";
if($query = $pdo->prepare($sql)){
$query->bindValue(':postid', $id);
$query->bindValue(':userid', $userid);
$query->execute();
$nlikes = $query->rowCount();
}
if($nlikes>0) {
$stmt2 = $pdo->prepare("DELETE FROM like WHERE userid=:userid AND postid=:bid");
$stmt2->bindValue(':userid', $userid);
$stmt2->bindValue(':bid', $id);
$stmt2->execute();
} else{
$stmt3 = $pdo->prepare("INSERT INTO like (userid,postid) VALUES (:userid, :postid)");
$stmt3->bindValue(':userid', $userid);
$stmt3->bindValue(':postid', $id);
$stmt3->execute();
}
}
?>
show_like.php
<?php
session_start();
ERROR_REPORTING(E_ALL & ~E_NOTICE);
include('php/connect.php');
include('php/function.php');
if (isset($_POST['showlike'])){
$id = $_POST['id'];
$sql = "SELECT count(*) FROM `like` WHERE postid = :postid";
if($query = $pdo->prepare($sql)){
$query->bindValue(':postid', $id);
$query->execute();
$nlikes = $query->fetchColumn();
echo $nlikes;
}
}
?>