Jump to content

[SOLVED] What's wrong with this if?


pneudralics

Recommended Posts

It inserts to the database but still echo the else instead of if.

if (!empty($_POST['postcomment'])){

	$postcommentq = "INSERT INTO usercomments (`from`, `to`, `comment`, `date`, `approve`) VALUES ('$cookieid', '$getid', '$postcomment', NOW(), 'N')";
	if (mysql_query($postcommentq)) {
	echo "<font color=\"red\">Comment posted. Waiting for approval.</font>";
	}
	else {
	echo "<font color=\"red\">There was an issue posting the comment.</font>";
	}

}

Link to comment
https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/
Share on other sites

try this

<?php
if (!empty($_POST['postcomment'])){
$postcommentq = "INSERT INTO usercomments (`from`, `to`, `comment`, `date`, `approve`) VALUES ('$cookieid', '$getid', '$postcomment', NOW(), 'N')";
if (mysql_query($postcommentq) == true) {
	echo "<font color=\"red\">Comment posted. Waiting for approval.</font>";
}else{
	echo "<font color=\"red\">There was an issue posting the comment.</font>";
}
}
?>

Those are backticks. I had a post earlier that the backticks solved another issue.

Anyways...even when I changed it to use with no backticks and change the field to: fromuser, touser

I still get the else instead of if.

 

If I add the == true it still echos else...but if I add == false it echos the if. The data is inserting, which I do not understand why it's echoing else if it's inserting.

try this

<?php
if (!empty($_POST['postcomment'])){
$postcommentq = "INSERT INTO usercomments (`from`, `to`, `comment`, `date`, `approve`) VALUES ('$cookieid', '$getid', '$postcomment', NOW(), 'N')";
if (mysql_query($postcommentq) == true) {
	echo "<font color=\"red\">Comment posted. Waiting for approval.</font>";
}else{
	echo "<font color=\"red\">There was an issue posting the comment.</font>";
}
}
?>

What about this -

<?php
if (!empty($_POST['postcomment'])){
   $postcommentq = "INSERT INTO usercomments (`from`, `to`, `comment`, `date`, `approve`) VALUES ('$cookieid', '$getid', '$postcomment', NOW(), 'N')";
   $result = mysql_query($postcommentq) or trigger_error(mysql_error(), E_USER_ERROR);
   if (mysql_num_rows($result) > 0) {
      echo "<font color=\"red\">Comment posted. Waiting for approval.</font>";
   }else{
      echo "<font color=\"red\">There was an issue posting the comment.</font>";
   }
}
?>

what Ken2k7 said

 

<?php
if (!empty($_POST['postcomment'])){
   $postcommentq = "INSERT INTO usercomments (`from`, `to`, `comment`, `date`, `approve`) VALUES ('$cookieid', '$getid', '$postcomment', NOW(), 'N')";
$result = mysql_query($postcommentq) or die(mysql_error());
   if ($result == true) {
      echo "<font color=\"red\">Comment posted. Waiting for approval.</font>";
   }else{
      echo "<font color=\"red\">There was an issue posting the comment.</font>";
   }
}
?>

 

EDIT: i'll also like to point out that its NOT inserting anything into the database!

Thanks for the help.

The >0 got me going towards resolving the issue. I eventually did:

 

if (!empty($_POST['postcomment'])){

	$postcommentq = mysql_query("INSERT INTO commentsuser (fromuser, touser, comment, date, approve) VALUES ('$cookieid', '$getid', '$postcomment', NOW(), 'N')");
	$postcommentr = mysql_query($postcommentq);
	if ($postcommentq > 0) {
	echo "<font color=\"red\">Comment posted. Waiting for approval.</font>";
	}
	else {
	echo "<font color=\"red\">There was an issue posting the comment.</font>";
	}

}
else {
echo "<font color=\"red\">Post comment is empty.</font>";
}

it's because in his condition he uses $...q which is the actual query result.

 

so basically, you two are scratching your heads because you fail to notice the difference between a q and a r :).  And OP's code technically works because it doesn't actually use that $...r var and so he's going to walk away thinking that's how its done and next time he's probably not going to typo the condition and then it really will break and he will have no idea why.

Is there something wrong with the following still?

 

When I echo $postcommentq it echos 1....so

if $postcommentq > 0 then we are good?

 

Meant it like this:

if (!empty($_POST['postcomment'])){

	$postcommentq = mysql_query("INSERT INTO commentsuser (fromuser, touser, comment, date, approve) VALUES ('$cookieid', '$getid', '$postcomment', NOW(), 'N')");
	//$postcommentr = mysql_query($postcommentq);
	if ($postcommentq > 0) {
	echo "<font color=\"red\">Comment posted. Waiting for approval.</font>";
	}
	else {
	echo "<font color=\"red\">There was an issue posting the comment.</font>";
	}

}
else {
echo "<font color=\"red\">Post comment is empty.</font>";
}

if you say so!

 

i'll also like to point out that its NOT inserting anything into the database!

 

EDIT:

Oh you have updated it

 

if ($postcommentq == true) {

would be better as $postcommentq is true but 1 is also true

so echoing $postcommentq would return 1

 

but if you done

var_dump($postcommentq)

your see its infact true

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.