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
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>";
}
}
?>

Link to comment
Share on other sites

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>";
}
}
?>

Link to comment
Share on other sites

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>";
   }
}
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>";
}

Link to comment
Share on other sites

WTF.. are you kidding me

 

#1 mysql_query returns a resource not the found set so thats usless!

#2 running a mysql_query or a resource from a mysql_query.. you have go to be kidding me

 

 

in any case i'm happy you have your solution!  ???

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
}

Link to comment
Share on other sites

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

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.