jr_cumbo Posted July 16, 2008 Share Posted July 16, 2008 Hi, Seem to be having some trouble with an if Statement <?php if (isset($_POST['loginsubmit'])); { if ($loginFoundUser==0) { echo "<p><font color=red>Username and Password Not Found</font></p>"; } } ?> The $loginFoundUser variable counts the number of rows in a sql query from a database,so if the user is found it would count 1 row from the form that is submitted, only one user can be entered into the form. So if the user is not found the row count is 0, so that all works fine, I just can not get the echo to appear when there is no user found. Any Help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/115012-solved-seem-to-be-having-some-trouble-with-an-if-statement-ampampgt/ Share on other sites More sharing options...
kenrbnsn Posted July 16, 2008 Share Posted July 16, 2008 Please show us were & how the $loginFoundUser variable is being set. Ken Quote Link to comment https://forums.phpfreaks.com/topic/115012-solved-seem-to-be-having-some-trouble-with-an-if-statement-ampampgt/#findComment-591443 Share on other sites More sharing options...
jr_cumbo Posted July 16, 2008 Author Share Posted July 16, 2008 <?php // *** Validate request to login to this site. if (!isset($_SESSION)) { session_start(); } $loginFormAction = $_SERVER['PHP_SELF']; if (isset($_GET['accesscheck'])) { $_SESSION['PrevUrl'] = $_GET['accesscheck']; } if (isset($_POST['loginuser'])) { $loginUsername=$_POST['loginuser']; $password=$_POST['loginpasswd']; $MM_fldUserAuthorization = ""; $MM_redirectLoginSuccess = "../index.html"; $MM_redirectLoginFailed = "login.php"; $MM_redirecttoReferrer = false; mysql_select_db($database_HTFConnection, $HTFConnection); ////////////////////////HERE IT IS////////////////////////////////////HERE IT IS/////////////////////////HERE IT IS/////////////////// $LoginRS__query=sprintf("SELECT user_name, user_passwd FROM user_signup WHERE user_name=%s AND user_passwd=md5(%s)", GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); $LoginRS = mysql_query($LoginRS__query, $HTFConnection) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); if ($loginFoundUser) { $loginStrGroup = ""; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //declare two session variables and assign them $_SESSION['MM_Username'] = $loginUsername; $_SESSION['MM_UserGroup'] = $loginStrGroup; if (isset($_SESSION['PrevUrl']) && false) { $MM_redirectLoginSuccess = $_SESSION['PrevUrl']; } header("Location: " . $MM_redirectLoginSuccess ); } else { header("Location: ". $MM_redirectLoginFailed ); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/115012-solved-seem-to-be-having-some-trouble-with-an-if-statement-ampampgt/#findComment-591451 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2008 Share Posted July 16, 2008 Ok, how does that chuck of code relate to the "if" statement? You seem to be only setting the variable if $_POST['loginuser'] is set. Ken Quote Link to comment https://forums.phpfreaks.com/topic/115012-solved-seem-to-be-having-some-trouble-with-an-if-statement-ampampgt/#findComment-591454 Share on other sites More sharing options...
DeanWhitehouse Posted July 16, 2008 Share Posted July 16, 2008 why have u got the post, surely then the if statement cant be run , the statement should be in the main bit of code. Quote Link to comment https://forums.phpfreaks.com/topic/115012-solved-seem-to-be-having-some-trouble-with-an-if-statement-ampampgt/#findComment-591456 Share on other sites More sharing options...
jr_cumbo Posted July 16, 2008 Author Share Posted July 16, 2008 Ok, how does that chuck of code relate to the "if" statement? You seem to be only setting the variable if $_POST['loginuser'] is set. Ken The $loginFoundUser is being set by the number of rows that are counted when the $_POST['loginuser'] is set. I'll have to try and figure out another way of doing it. What I'm trying to do is check if the user exist in the database and if not display a no user found error. Quote Link to comment https://forums.phpfreaks.com/topic/115012-solved-seem-to-be-having-some-trouble-with-an-if-statement-ampampgt/#findComment-591459 Share on other sites More sharing options...
jr_cumbo Posted July 16, 2008 Author Share Posted July 16, 2008 I changed the code from above to this <?php $user = $_POST['loginuser']; $count = mysql_num_rows(mysql_query("SELECT user_name FROM user_signup WHERE user_name='".$user."'")); if (isset($_POST['loginsubmit'])) { if ($count == 0) { echo "<p><font color=red>Username and Password Not Found</font></p>"; } } ?> Can anyone see anything wrong with this? Any Help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/115012-solved-seem-to-be-having-some-trouble-with-an-if-statement-ampampgt/#findComment-591507 Share on other sites More sharing options...
DeanWhitehouse Posted July 16, 2008 Share Posted July 16, 2008 is it not working?? try removing the if statement Quote Link to comment https://forums.phpfreaks.com/topic/115012-solved-seem-to-be-having-some-trouble-with-an-if-statement-ampampgt/#findComment-591508 Share on other sites More sharing options...
jr_cumbo Posted July 16, 2008 Author Share Posted July 16, 2008 is it not working?? try removing the if statement No its not working the way I need it to. If I remove the if statement it will display the echo message, I only want to display that message if $count = 0 Quote Link to comment https://forums.phpfreaks.com/topic/115012-solved-seem-to-be-having-some-trouble-with-an-if-statement-ampampgt/#findComment-591513 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2008 Share Posted July 16, 2008 You need to do some debugging on your own. I would do something like this: <?php echo '<pre>$_POST["loginuser"] :' .print_r($_POST['loginuser'],true) . '</pre>'; $q = "SELECT user_name FROM user_signup WHERE user_name='" . mysql_real_escape_string($_POST['loginuser']) . "'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); $count = mysql_num_rows($rs); echo '$count: ' . $count . '<br>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/115012-solved-seem-to-be-having-some-trouble-with-an-if-statement-ampampgt/#findComment-591562 Share on other sites More sharing options...
jr_cumbo Posted July 16, 2008 Author Share Posted July 16, 2008 Thanks Ken I'll give that a go. I'll have to keep trying to I get it. I'm using eclipse to debug with Xdebug. It seems as tho it goes though all the scripts I have wrote once and everything works fine the var $count = 1 if found and = 0 if not found, but for some reason it is going though the code a second time and then setting the var $count to false that is what I think is the problem well I'm sure that would be the problem. I'll have to find out why its looping around a second time. Thanks a heap for helping me!! Quote Link to comment https://forums.phpfreaks.com/topic/115012-solved-seem-to-be-having-some-trouble-with-an-if-statement-ampampgt/#findComment-591578 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.