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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Barrie
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Keep getting invalid login when i try to login

    any help on why its not letting log in would be appreciated, thanks

    PHP Code:
    <?php
    // get login values & hash the password
    $username = ($_POST['username']);
    $password =  sha1($_POST['password']);

    //  connect to the database
    $conn mysqli_connect('','''''') or die('connection error');

    // write a sql query
    $sql ="SELECT id FROM admins WHERE username = '$username' AND password = '$password'";
    echo 
    $sql;
    // execute the sql and store the query result
    $result mysqli_query($conn$sql);
    // count the number of rows in the database 
    $count mysqli_num_rows($result);
    echo 
    $count;
    // check the count to see the amount of matches 

    if($count == 1)
    {

        echo 
    'logged in successfully';
        
    // get first record from query
        
    $row mysqli_fetch_array($result);

        
    // store the id in the session object
        
    session_start(); 
        
    $_SESSION['user_id'] = $row['id']; 

    }
    else
    {
        echo 
    'Invalid Login'
        
    }

    mysqli_close($conn);

    ?>

  • #2
    New to the CF scene
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try removing the () like

    $username = $_POST['username'];

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    PHP Code:
    session_start(); 
    the above statement must occur before any echo's/output, so place it as the first line.

    PHP Code:
    $row mysqli_fetch_array($resultMYSQLI_ASSOC); 
    or MYSQLI_BOTH, MYSQLI_NUM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    after removing the brackets that bogan suggests
    try this as your query:

    HAVING instead of AND
    PHP Code:
    $sql ="SELECT id FROM admins WHERE username = '".$username."' HAVING password = '".$password."'"


  •  

    Posting Permissions

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