Jump to content

Recommended Posts

		if(isset($_POST['loginForm'])) {
// Define Variables
#################################
$username = $_POST['username'];
$password = $_POST['password'];
$hashpw = sha1($password);
#################################
//Check to see they Entered Something



// Execute Login
$result = mysql_query("SELECT * FROM members WHERE username='$username'");
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
if($username == "") {
$error = "There was NO username entered!";
}elseif(mysql_num_rows($result) == 0) {

$error = "The username you entered is non-existent in our database.";

} elseif($line['password'] != $hashpw) {
	$error = "Your password is not correct.";
} else {
$_SESSION['id'] = $line['userid'];
$_SESSION['username'] = $line['username'];
$_SESSION['password'] = $hashpw;
$_SESSION['email'] = $line['email'];
    $_SESSION['type'] = $line['type'];
$_SESSION['name'] = $line['name'];
$_SESSION['favgame'] = $line['favgame'];
$_SESSION['favconsole'] = $line['favconsole'];
$_SESSION['avatar'] = $line['avatar'];
$_SESSION['photo'] = $line['photo'];
$_SESSION['reg_date'] = $line['reg_date'];


redirect(0, '', $_SERVER["HTTP_REFERER"]);
	}

if(isset($error)) {
	echo "<td>";

		echo $error."<br />";

	echo "<a href=\"".$_SERVER["HTTP_REFERER"]."\"><< Go Back</a></td>
	<td></td>
		</tr>";
}
}
} 

 

 

when I had

if($username == "") {
$error = "There was NO username entered!";
}

 

at the top before the while statement... and I didn't enter a name it echoed taht error, but when i would enter a username such as "bobby269" that didn't exist, it wouldn't echo the "User non-existent" error.

 

i tryed moving the while statement brackets and switching up the code at least 15 times in a total of about 30 minutes but nothing seemed to work..

 

any help appreciated :)

 

Thanks,

Mike

Link to comment
https://forums.phpfreaks.com/topic/83543-solved-not-echoing-error/
Share on other sites

You need to check both an empty condition and num_rows before the while loop, and if any errors, don't bother running a loop over a non-existent record set.

 

<?php
if ( isset($_POST['loginForm']) && $_POST['loginForm'] == 'someVal' ) {
    // Define Variables
    #################################
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $hashpw = sha1($password);
    #################################
    //Check to see they Entered Something
    if( $username == "" ) {
        $error = "There was NO username entered!";
    } else {
        // Execute Login
        if ( !$result = mysql_query("SELECT * FROM `members` WHERE `username` = '$username' LIMIT 0,1") ) {
            die('MySQL Error: ' . mysql_error());
        }
        if ( mysql_num_rows($result) > 0 ) {
            $line = mysql_fetch_assoc($result);
            if ( $line['password'] != $hashpw ) {
                $error = "Your password is not correct.";
            } else {
                // login successful
                $_SESSION['id'] = $line['userid'];
                $_SESSION['username'] = $line['username'];
                $_SESSION['password'] = $hashpw;
                $_SESSION['email'] = $line['email'];
                $_SESSION['type'] = $line['type'];
                $_SESSION['name'] = $line['name'];
                $_SESSION['favgame'] = $line['favgame'];
                $_SESSION['favconsole'] = $line['favconsole'];
                $_SESSION['avatar'] = $line['avatar'];
                $_SESSION['photo'] = $line['photo'];
                $_SESSION['reg_date'] = $line['reg_date'];
                redirect(0, '', $_SERVER["HTTP_REFERER"]);
            }
        } else {
            $error = "The username you entered is non-existent in our database.";
        }
    }
} else {
    $error = "You cannot access this Login Page in that manner.";
}
// if we got here, there was an error, so no need to check with isset
echo "<td>$error<br /><a href=\"" . $_SERVER["HTPP_REFERRER"] . "\"><< Go Back</a></td>
<td> </td>
</tr>";

// whatever else you want here

// EOF
?>

 

I don't know what your form sends with $_POST['loginForm'], but you'll need to replace 'someVal' in the first IF test with whatever that value is. If left as you had it, $_POST['loginForm'] = 'foobar' would've satisfied isset...

 

Anyways, this script isn't thoroughly tested, but it should be closer to what you want.

 

PhREEEk

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.