3raser Posted April 8, 2011 Share Posted April 8, 2011 I'm sure you can do this in PHP, but for some reason when I view news.php - It automatically tells me I liked the post, when I haven't even clicked like yet! while($fetch = mysql_fetch_assoc($query)) { if($_GET['like'] == $fetch['id']) { $like = mysql_real_escape_string($_GET['like']); $has_liked = mysql_query("SELECT * FROM likes WHERE id = '$like' AND username = '". $_SESSION['user'] ."'"); if(mysql_num_rows($has_liked) > 0) { echo " <div id='post-1' class='post'> <h2 class='title'><font color='white'>#". $i." ".$fetch['title'] ."</font> <h3 class='date'>". $fetch['date'] ."</h3> <div class='entry'> <p>". nl2br(stripslashes($fetch['content'])) ."</p> </div> <p class='meta'><b>You've already liked this announcement.</b></p> </div> "; } else { mysql_query("UPDATE news SET likes = likes + 1 WHERE id = '". $_GET['id'] ."'"); echo " <div id='post-1' class='post'> <h2 class='title'><font color='white'>#". $i." ".$fetch['title'] ."</font> <h3 class='date'>". $fetch['date'] ."</h3> <div class='entry'> <p>". nl2br(stripslashes($fetch['content'])) ."</p> </div> <p class='meta'><b>You liked this announcement.</b></p> </div> "; } } else { echo " <div id='post-1' class='post'> <h2 class='title'><font color='white'>#". $i." ".$fetch['title'] ."</font> <h3 class='date'>". $fetch['date'] ."</h3> <div class='entry'> <p>". nl2br(stripslashes($fetch['content'])) ."</p> </div> <p class='meta'><a href='news.php?like=". $fetch['id'] ."'>Like</a></p> </div> "; } } Quote Link to comment https://forums.phpfreaks.com/topic/233042-if-statements-inside-while/ Share on other sites More sharing options...
drisate Posted April 8, 2011 Share Posted April 8, 2011 Just a guess like that ... if $_GET['like'] and $fetch['id'] are both empty it might also count as a like. try testing with isset() Quote Link to comment https://forums.phpfreaks.com/topic/233042-if-statements-inside-while/#findComment-1198546 Share on other sites More sharing options...
3raser Posted April 8, 2011 Author Share Posted April 8, 2011 Just a guess like that ... if $_GET['like'] and $fetch['id'] are both empty it might also count as a like. try testing with isset() Sadly, isset() didn't work. If this is what you meant: if(isset($_GET['like']) == isset($fetch['id'])) Quote Link to comment https://forums.phpfreaks.com/topic/233042-if-statements-inside-while/#findComment-1198550 Share on other sites More sharing options...
drisate Posted April 8, 2011 Share Posted April 8, 2011 try this if(isset($_GET['like']) and isset($fetch['id']) and $_GET['like']==$fetch['id']){ Quote Link to comment https://forums.phpfreaks.com/topic/233042-if-statements-inside-while/#findComment-1198552 Share on other sites More sharing options...
3raser Posted April 8, 2011 Author Share Posted April 8, 2011 try this if(isset($_GET['like']) and isset($fetch['id']) and $_GET['like']==$fetch['id']){ Hah, worked, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/233042-if-statements-inside-while/#findComment-1198556 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.