nicangeli Posted July 18, 2007 Share Posted July 18, 2007 Hello. Please take a look at the code below and if any of you could tell me why $error['false'] is never getting echoed that would be a great help. Basically the script is a login script and i am testing the POST variables agains things and storing the error messages in a array. I then attempt to echo the array out foreach loop but ['false'] never works. <?php session_start(); require('areatop.php'); require('includes/colleft.php'); if (isset($_POST['submit'])) { $error = array(); if (empty($_POST['username'])) { $error['username'] = "Please enter a username"; } if (empty($_POST['password'])) { $error['password'] = "Please enter a password"; } if (count($error) == 0) { // FORM PROCCESSING HERE $query = mysql_query("query here"); if (!$query) { echo "NO QUERY" . mysql_error(); } else { $number = mysql_num_rows($query); if ($number == "1") { echo "Logged In"; } else { $error['false'] = "Incorrect login details"; } } } else { echo '<div class="formerror"><img src="includes/images/triangle_error.gif" />Please correct the following errors:<ul>'; foreach ($error as $e) { echo "<li>$e</li><br>"; } if (!empty($error['false'])) { echo $error['false']; } echo '</ul></div><br>'; } } ?> <p> <form method="post" action="<?php echo $PHP_SELF; ?>"> <label for="username">Username: </label> <input type="text" name="username" value="<?php echo $_POST['username']; ?>" class="<?php if (!empty($error['username'])) { echo 'formerror'; } elseif (!empty($error['false'])) { echo 'formerror'; } ?> "> <label for="password">Password: </label> <input type="password" name="password" class="<?php if (!empty($error['password'])) { echo 'formerror'; } elseif (!empty($error['false'])) { echo 'formerror'; } ?> "> <br> <input type="submit" name="submit" value="Log In" /> </form> Quote Link to comment Share on other sites More sharing options...
clearstatcache Posted July 19, 2007 Share Posted July 19, 2007 bwt replacind 'false' with another variable name.... Quote Link to comment Share on other sites More sharing options...
nicangeli Posted July 19, 2007 Author Share Posted July 19, 2007 thanks for the reply but that did nothing. any one else? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 19, 2007 Share Posted July 19, 2007 You would probably find the error a whole stack easier to spot if you intended your code: <?php session_start(); require('areatop.php'); require('includes/colleft.php'); if (isset($_POST['submit'])) { $error = array(); if (empty($_POST['username'])) { $error['username'] = "Please enter a username"; } if (empty($_POST['password'])) { $error['password'] = "Please enter a password"; } if (count($error) == 0) { // FORM PROCCESSING HERE $query = mysql_query("query here"); if (!$query) { echo "NO QUERY" . mysql_error(); } else { $number = mysql_num_rows($query); if ($number == "1") { echo "Logged In"; } else { $error['false'] = "Incorrect login details"; } } } else { echo '<div class="formerror"><img src="includes/images/triangle_error.gif" />Please correct the following errors:<ul>'; foreach ($error as $e) { echo "<li>$e</li><br>"; } if (!empty($error['false'])) { echo $error['false']; } echo '</ul></div><br>'; } } ?> Basically, $error['false'] can only ever be set if there are no errors in the username and password, since all of the database quering is contained in the codeblock: <?php if (count($error) == 0) { //here is all the quering stuff and here is were $error['false'] is set } ?> You then add an else statement where the errors are displayed. <?php if (count($error) == 0) { //here is all the quering stuff and here is were $error['false'] is set }else{ //here is where you echo the errors } ?> So for your false error to be displayed, both parts of this would have to be executed, which is , of course, im possible( since count($error) cant be 0 and not 0). Hope that makes some sense. It's kinda difficult for me to suggest the fix. It depends how you want your script to work. Quote Link to comment 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.