simmsy Posted April 16, 2010 Share Posted April 16, 2010 Hi i was just wondering if anyone could tell me why i get this javascript alert when the username is entered wrong but it just refreshes page when the password is entered wrong and doesnt display error? Heres the code: // Check matching of username and password. $result=mysql_query("select username, password from users where username='$username' and password='$md5_password'"); while($row = mysql_fetch_array($result)) { $user = $row['username']; $password = $row['password']; if(($username==$user)AND($password==$md5_password)){ // If match. session_register("username"); // Craete session username. header("location:index.php"); // Re-direct to main.php exit; }else{ // If not match. echo "<script language=\"JavaScript\">\n"; echo "alert('Username or Password was incorrect!');\n"; echo "window.location='login.php'"; echo "</script>"; please help? Link to comment https://forums.phpfreaks.com/topic/198797-password-error-alert-message/ Share on other sites More sharing options...
ChemicalBliss Posted April 16, 2010 Share Posted April 16, 2010 You matched both in mysql, you dont really need that second check. // Check matching of username and password. $result=mysql_query("select username, password from users where username='$username' and password='$md5_password'"); // Check the amount of rows returned: if(mysql_num_rows($result) >= 1){ // Put first row into $row $row = mysql_fetch_array($result); $user = $row['username']; $password = $row['password']; $_SESSION['username'] = $user; // Create session username, and set it. header("location:index.php"); // Re-direct to main.php exit; }else{ echo "<script language=\"JavaScript\">\n"; echo "alert('Username or Password was incorrect!');\n"; echo "window.location='login.php'"; echo "</script>"; } -cb- Link to comment https://forums.phpfreaks.com/topic/198797-password-error-alert-message/#findComment-1043368 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.