Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Login error

  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Login error

    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
    Code:
    <?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
    Code:
    <?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.

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,139
    Thanks
    7
    Thanked 257 Times in 256 Posts
    Code:
    $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:

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

  • Users who have thanked tracknut for this post:

    hans_cellc (10-09-2012)

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Absolutely correct, I feel so STUPID. But have learnt an important lesson.

    Thanks a lot.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •