Jump to content

If statements inside while?


3raser

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.