0

I'm trying to make a simple login function using PDO and SQL Server but for some reason I keep getting this error. I'm not sure where all these parameters are coming from, so I'm not quite sure how to fix this. Here is the code:

DB Connection:


function dbconnect()
{
    $serverName = "Server";
    $databaseName = "Name";
    
    try {
        
        $db = new PDO("sqlsrv:Server = $serverName; Database = $databaseName", "Username", "Password");
        $db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
    } catch (PDOException $ex) {

        $ex ->getMessage();
        
    }
    
    return $db;
    
}

Login Function:

include (__DIR__ . '/DB_Connect.php');

function checkLogin ($username, $password)
{
    $db = dbconnect();
    
    $stmt = $db->prepare("SELECT * FROM Users WHERE Username = ':username' and Password = ':password'");
    
    $binds = array
    (
        ":username" => $username,
        ":password" => sha1($password)
    );
    
    if ($stmt->execute($binds))
    {
        return ("True");
    }
    else
    {
        return ("False");
    }
    
    return ($stmt->rowCount() > 0);
}

Session Variable:

<?php

    session_start();
    
    include __DIR__ . '/Models/Models_Application.php';
    include __DIR__ . '/Functions.php';
    
    $results = "";
    $username = filter_input(INPUT_POST, 'Username');
    $password = filter_input(INPUT_POST, 'Password');
    $action = filter_input(INPUT_POST, 'action');
    
    if( $action === "Login" && checkLogin($username, $password) == "True")
    {
        $_SESSION['logged_In'] = "True";
        header('location:Home.php');
    }
    else
    {
        if(isPostRequest())
        {
            $results = "Invalid Username or Password, please try again<br>";
        }
    }
?>

That's pretty much all the code besides the form and the page you get to once you've logged in. Any help I can get would be greatly appreciated.

  • When you are preparing a query the single quotes are not required SO `Username = :username and Password = :password` – RiggsFolly Jul 09 '20 at 18:34
  • 1
    Please have a read about how to use [`password_hash()`](https://stackoverflow.com/questions/30279321/how-to-use-phps-password-hash-to-hash-and-verify-passwords) which is a more up to date method. – Nigel Ren Jul 09 '20 at 18:34

0 Answers0