Glenskie Posted April 10, 2014 Share Posted April 10, 2014 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"); } } Quote Link to comment https://forums.phpfreaks.com/topic/287684-php-like-system/ Share on other sites More sharing options...
trq Posted April 10, 2014 Share Posted April 10, 2014 What have you done to debug your code? Quote Link to comment https://forums.phpfreaks.com/topic/287684-php-like-system/#findComment-1475730 Share on other sites More sharing options...
fastsol Posted April 12, 2014 Share Posted April 12, 2014 Most likely your first query is failing since you are not giving any value to $id for the uid in the query. Quote Link to comment https://forums.phpfreaks.com/topic/287684-php-like-system/#findComment-1475837 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.