Jump to content

php like system.


Glenskie

Recommended Posts

I have a php and jquery like system that doesn't work for some reason? When i click on the button to like it does nothing not even gives me an error please help thank you ! Here is my code that I am using and maybe you will see something that I didn't? The first part of the php code works it shows the like button so I know that's right but when you click it, it does nothing ...


PHP:



$likes = mysql_query("SELECT lid FROM likes WHERE uid='$id' AND imid='$photoid'");
if(mysql_num_rows($likes)==0){
$likethis = '<a href="#" id="'.$photoid.'" title="like"><div class="like"></div></a>';
}else{
$unlikethis = '<a href="#" id="'.$photoid.'" title="unlike"><div class="unlike"></div></a>';
}

JQUERY:



<script type="text/javascript">
$(document).ready(function(){
$(document).bind('click', '.like', function(){
if($(this).attr('title')=='like'){
$.post('like.php',{imid:$(this).attr('id'),action:'like'},function(){
$(this).text('unlike');
$(this).attr('title','Unlike');
});
}else{
if($(this).attr('title')=='unlike'){
$.post('like.php',{imid:$(this).attr('id'),action:'unlike'},function(){
$(this).text('like');
$(this).attr('title','like');
});
}
}
});
});
</script>

like.php:



$imid=$_POST['imid'];
$action=$_POST['action'];
if ($action=='like'){
$sql= mysql_query("SELECT * FROM likes WHERE imid=$imid and uid=$id");
$matches=$sql->rowCount();
if($matches==0){
$sql= mysql_query("INSERT INTO likes (imid, uid) VALUES($imid, $id)");
$sql= mysql_query("UPDATE photos SET likes=likes+1 WHERE id=$imid");
}else{
die("There is No Image With That ID");
}
}
if ($action=='unlike'){
$sql= mysql_query("SELECT * FROM likes WHERE imid=$imid and uid=$id");
$matches=$sql->rowCount();
if ($matches!=0){
$sql=mysql_query("DELETE FROM likes WHERE imid=$imid AND uid=$id");
$sql=mysql_query("UPDATE photos SET likes=likes-1 WHERE id=$imid");
}
}

Link to comment
https://forums.phpfreaks.com/topic/287684-php-like-system/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.