Jump to content

Code is not working trying to login using SQL SERVER


TStyles

Recommended Posts

I am new to PHP and this is the first try to code.
I have a login Form with Email and password,when the user submits the form ,there should be a connection to a MS SQL Server and then Check to see if the values are valid.
check1 is being displayed but everything after is not.
Is this the correct way to connect to MS sql server?Most articles on the internet are using MYSQL so i can't find much info on SQL Server.

PHP code is present on the same page as the html.
I am desperate to know why the code isn't working,Thanks for the help.
Here is what i have tried so Far:

HTML code:

<form class="form-horizontal" method="post" action="Login.php">    
<input type="email" id="inputEmail" class="form-control" name="username" placeholder="Email address" required >
<input type="password" id="inputPassword" class="form-control" name="password" placeholder="Password" required >
<button class="btn btn-lg btn-primary btn-block" type="submit" name='Submit' value='Submit'>Login</button>
 </form>

PHP Code:

if (isset($_POST['Submit']))
{
    Login();
}
function Login()
{   
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    $serverName = "TAREX-09\MSSQLSERVER"; //name of server(locally)
    $connectionOptions = array("Database" => "Insurance");
    echo 'check1';
    //Connect using Windows Authentication.
    $conn = sqlsrv_connect($serverName, $connectionOptions);
    echo 'check2';
    if (!$conn)
    {
        echo 'failed';
        //die(FormatErrors(sqlsrv_errors()));
    }
 
    $tsql = "SELECT EmailAddress,Password
               FROM Customer_Detail
               WHERE EmailAddress=$username AND Password=$password ";
    $getlogin = sqlsrv_query($conn, $tsql);
    if ($getlogin === false)
    {
        echo 'Login Failed';
    } else
    {
        echo 'Login Success';
    }
Link to comment
Share on other sites

Check the PHP error log. The sqlsrv_* functions may not be available in your current setup.

 

In generally, I don't recommend using MS SQL, unless you absolutely must. Compared to a common open-source database system like MySQL/MariaDB or PostgreSQL, you'll have a lot more trouble and a lot less support. If you do choose it, be prepared for your own research.

 

 

 

You have only the database name

 

He's using Windows Authentication, and even if the function fails, this doesn't crash the whole script.

Edited by Jacques1
Link to comment
Share on other sites

Check the PHP error log. The sqlsrv_* functions may not be available in your current setup.

 

In generally, I don't recommend using MS SQL, unless you absolutely must. Compared to a common open-source database system like MySQL/MariaDB or PostgreSQL, you'll have a lot more trouble and a lot less support. If you do choose it, be prepared for your own research.

 

 

 

 

He's using Windows Authentication, and even if the function fails, this doesn't crash the whole script.

 

I kinda have to use MS SQL, i'm not getting any error when i attempr to login,it's just that only the first echo is working ... i already have the sql drivers for php installed and configured. I'm using IIS too.

Edited by TStyles
Link to comment
Share on other sites

PHP doesn't just crash for no reason. Have you checked the error log of IIS? Errors don't necessarily pop up on the screen, but they do get logged -- unless your PHP configuration is completely broken.

Just checked for error logs and there are none in the inetpub directories.

How can i see if the sqlsrv_* functions are working correctly?

I'll uninstall then reinstall the sql drivers for php,and see if it works.

Edited by TStyles
Link to comment
Share on other sites

PHP doesn't just crash for no reason. Have you checked the error log of IIS? Errors don't necessarily pop up on the screen, but they do get logged -- unless your PHP configuration is completely broken.

 

 

You have only the database name

i want to make sure i have the correct sql drivers installed. can you check it for me please(links are safe ,images uploaded to imgur)
here's the files i downloaded from Microsoft's website:  http://imgur.com/a/7O84l
And here i moved the non thread safe x64 dlls to php ext directory: http://imgur.com/a/8Hz71
Finally i added them to the extensions in php.ini :  http://imgur.com/a/TIGoB
 
anything more i need to do? because i still don't have anything related to sqlserv when i go to phpinfo() page on the server!
Edited by TStyles
Link to comment
Share on other sites

Did you match the dll versions properly (nts vs ts, x86 vs x64)?

 

Did you ensure to check the System requirements? Namely:

The correct version of the Microsoft ODBC Driver for SQL Server or SQL Server Native Client is required on the computer where PHP is running.

For version 4 you need Microsoft ODBC Driver 11 for SQL Server or Microsoft ODBC Driver 13 for SQL Server

 

Lastly, I'd suggest using the PDO interface, not the sqlsrv_* functions. It's easier.

//NULL username/password for Windows auth.
$db = new PDO('sqlsrv:server=(local);Database=sample', null, null, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$stmt = $db->query('SELECT fname, lname FROM people');
foreach ($stmt as $row){
    echo $row['fname'].' '.$row['lname'].'<br>';
}
Edited by kicken
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.