...

View Full Version : Login error



hans_cellc
10-09-2012, 09:06 PM
Please assist:
I have created a database with a tb_user.

I have a form to checklogin.php with the code below:


The connect_db.php

<?PHP
// I am setting all my variables to make it easier to change code at a later stage by just cganging the variables
$connect_error = "Could not connect to Database";
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_passw = "";
$mysql_db = "games";

// Using a f statement with the not inside that if the DB does not exist or could not connect
// it will die with an error message as per the variable, I used the @ sign to silence the normal error message
if (!@mysql_connect($mysql_host, $mysql_user, $mysql_passw) OR !@mysql_select_db($mysql_db)) {
die($connect_error);
}

?>

The checklogin.php

<?PHP
// Import the connect to DB code as required as we need it and can not continue with out it.
require 'connect_db.php';

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

// Table name variable
$tb_name = "tb_user";

// Set the Query within a variable
$query = "SELECT * FROM $tb_name WHERE username='$username' AND password='$password'";
$result = mysql_query($query);

// Mysql_num_row is counting table row
$count = mysql_num_rows($result);

// Create a if statement to check if results returned and if not to display an error
if($count == 1) {
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Username or Password INCORRECT!";
}

?>

It connects to the DataBase however it gives the following error:

( ! ) Notice: Undefined variable: username in C:\wamp\www\exam\checklogin.php on line 19
Call Stack
# Time Memory Function Location
1 0.0022 143480 {main}( ) ..\checklogin.php:0

( ! ) Notice: Undefined variable: password in C:\wamp\www\exam\checklogin.php on line 19
Call Stack
# Time Memory Function Location
1 0.0022 143480 {main}( ) ..\checklogin.php:0
Username or Password INCORRECT!

username and password both do exist in my DataBase in column 5 and 6.

Pulling my hair here I am sure this must work.

tracknut
10-09-2012, 09:21 PM
$query = "SELECT * FROM $tb_name WHERE username='$username' AND password='$password'";


You're putting the entered values in $myusername and $mypassword. Shouldn't this be:



$query = "SELECT * FROM $tb_name WHERE username='$myusername' AND password='$mypassword'";


Dave

hans_cellc
10-09-2012, 09:32 PM
Absolutely correct, I feel so STUPID. But have learnt an important lesson.

Thanks a lot.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum