简体   繁体   中英

Sqlserver connection error for login system

I am trying to create a login system for my server. I have used similar code for my local website using xampp but now I am trying for an external server. The error i am getting is this.

Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near ' '. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near ' '. ) )

and my code is below:

<?php

session_start();

$inputuser = $_REQUEST['Username'];
$inputpass = $_REQUEST['Password'];

$Server = "MyServer";
$user = "user";
$password = "pass";
$database = "mydb";
$table = "users";

$connectionInfo = array( "Database"=>$database,"UID"=>$user, "PWD"=>$password);

$link = sqlsrv_connect($Server, $connectionInfo);

if ($link === false) {
    echo "Connection failed. \n";
    die(print_r(sqlsrv_errors(), true));
}


$query = "SELECT `Username` FROM `onlinereporting` . `users` WHERE `Username` = '$inputuser'"; 
$querypass = "SELECT `Password` FROM `onlinereporting` . `users` WHERE `Password` ='$inputpass'";

$result = sqlsrv_query($link,$query);
$resultpass = sqlsrv_query($link,$querypass);

if ($result === false) {
    die(print_r(sqlsrv_errors(), true));
}

$row = sqlsrv_fetch_array($result);
$rowpass = sqlsrv_fetch_array($resultpass);

$serveruser = $row["Username"];
$serverpass = $rowpass["Password"];


if ($serveruser And $serverpass) {

    header('Location: index.html');

}

else {  

    header('Location: FailedPage.html');

}
?>

The connection is working fine, the only problem occurs when I try to check if the result is false.

When i am not trying to check the $result, everything I insert is considered as a wrong username and password.

Any help or advice is welcome.

我相信原因是您使用反引号来引用表名和列名,而在SQL Server名称中使用"来引用。尝试将反引号更改为双引号,即:

$query = "SELECT \"Username\" FROM \"onlinereporting\" . \"users\" WHERE \"Username\" = '$inputuser'"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM