pneudralics Posted May 22, 2009 Share Posted May 22, 2009 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>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/ Share on other sites More sharing options...
Hybride Posted May 22, 2009 Share Posted May 22, 2009 Remove the quotes from your $postcommentq for the rows, not the values ('from' -> from). And when you're doing SQL statements, you should put a mysql_error() at the end of your query so you can tell what the error is exactly. Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/#findComment-840343 Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/#findComment-840344 Share on other sites More sharing options...
pneudralics Posted May 23, 2009 Author Share Posted May 23, 2009 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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/#findComment-840346 Share on other sites More sharing options...
Ken2k7 Posted May 23, 2009 Share Posted May 23, 2009 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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/#findComment-840348 Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/#findComment-840351 Share on other sites More sharing options...
pneudralics Posted May 23, 2009 Author Share Posted May 23, 2009 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/#findComment-840362 Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 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! ??? Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/#findComment-840364 Share on other sites More sharing options...
Ken2k7 Posted May 23, 2009 Share Posted May 23, 2009 O.O I'm with MadTechie. I don't get that code at all. ??? Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/#findComment-840377 Share on other sites More sharing options...
.josh Posted May 23, 2009 Share Posted May 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/#findComment-840639 Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 I just gave up, hence in any case i'm happy you have your solution! ??? Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/#findComment-840644 Share on other sites More sharing options...
pneudralics Posted May 23, 2009 Author Share Posted May 23, 2009 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/#findComment-840656 Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/159327-solved-whats-wrong-with-this-if/#findComment-840658 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.