Jump to content

[SOLVED] not echoing $error..


MikeDXUNL

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.