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> "; } } 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() 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'])) 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']){ 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. Link to comment https://forums.phpfreaks.com/topic/233042-if-statements-inside-while/#findComment-1198556 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.