nathanmaxsonadil Posted August 23, 2007 Share Posted August 23, 2007 I've made a login script that checks it the username and or password is correct however it does not do anything if the username is incorecct here's my code to check the username: $checklogin2 = "SELECT * FROM users WHERE username = '". $_POST['username']. "'"; $checklogin = mysql_query($checklogin2) or die; while ($row = mysql_fetch_array($checklogin, MYSQL_ASSOC)) { if (mysql_num_rows($checklogin) == 0) { $_SESSION['error'] = '1'; header("Location: ".$_SERVER['HTTP_REFERER']); } and here's the login form echo '<h2 id="round">Login</h2> <form method="post" action="login.php"> <fieldset> <legend>Sign-In</legend>'; if($_SESSION['error'] == 1) { echo "Your username or password is incorrect<br/><br/>"; unset($_SESSION['error']); } echo '<label for="username">Username:</label> <input type="text" name="username" value="" /> <label for="password">Password:</label> <input type="password" name="password" value="" /> <input type="submit" name="submit" value="Sign In" /> <p><a href="#">Forgot your password?</a><br/><a href="#">Register for a free account<a/></p> </fieldset> </form> '; Quote Link to comment https://forums.phpfreaks.com/topic/66397-solved-username-check-does-not-work/ Share on other sites More sharing options...
lemmin Posted August 23, 2007 Share Posted August 23, 2007 Probably because there is no referrer. Try 'PHP_SELF'. Quote Link to comment https://forums.phpfreaks.com/topic/66397-solved-username-check-does-not-work/#findComment-332353 Share on other sites More sharing options...
nathanmaxsonadil Posted August 23, 2007 Author Share Posted August 23, 2007 I tried test.com but it did'nt do it either Quote Link to comment https://forums.phpfreaks.com/topic/66397-solved-username-check-does-not-work/#findComment-332355 Share on other sites More sharing options...
lemmin Posted August 23, 2007 Share Posted August 23, 2007 I would guess that $checklogin always has more than one row. Print the output from the query and see what is there. Quote Link to comment https://forums.phpfreaks.com/topic/66397-solved-username-check-does-not-work/#findComment-332361 Share on other sites More sharing options...
nathanmaxsonadil Posted August 23, 2007 Author Share Posted August 23, 2007 $checklogin does not have more that 1 row I tried puting else's after my code but None of the else's did anything Quote Link to comment https://forums.phpfreaks.com/topic/66397-solved-username-check-does-not-work/#findComment-332374 Share on other sites More sharing options...
trq Posted August 23, 2007 Share Posted August 23, 2007 Lets eliminate one problem at a time. Get rid of the redirect and use... <?php $checklogin2 = "SELECT * FROM users WHERE username = '". $_POST['username']. "'"; if ($checklogin = mysql_query($checklogin2)) { if (!mysql_num_rows($checklogin)) { echo "Username not found"; } else { $row = mysql_fetch_assoc($checklogin); } } else { echo mysql_error(); } ?> Notice you need to call mysql_num_rows() before mysql_fetch* anyway. You'll get errors the way you had it. Quote Link to comment https://forums.phpfreaks.com/topic/66397-solved-username-check-does-not-work/#findComment-332380 Share on other sites More sharing options...
nathanmaxsonadil Posted August 23, 2007 Author Share Posted August 23, 2007 I put the if before the mysql find array and it worked Quote Link to comment https://forums.phpfreaks.com/topic/66397-solved-username-check-does-not-work/#findComment-332386 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.