Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
I am desperate to know why the code isn't working,please help me guys.
UPDATE : i want to make sure i have the correct sql drivers installed. can you check it for me (links are safe ,imagesuploaded to imgur)
here's the files i downloaded from Microsoft's website: link
And here i moved the non thread safe x64 dlls to php ext directory: link
Finally i added them to the extensions in php.ini : link

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!

Here is what i have tried so Far:

What I have tried:

HTML
<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>

Here is the PHP Code:
PHP
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';
    }
Posted
Updated 12-Aug-16 5:21am
v6
Comments
phil.o 11-Aug-16 16:13pm    
The connection to the database is made under the account which runs the Apache service. Did you grant this account access rights on the database?
xTMx9 11-Aug-16 16:52pm    
i'm using IIS to connect to database. IIS can already access the database in another application(Asp.net) so access is already granted,is it right?
xTMx9 11-Aug-16 16:58pm    
I have this login in the sql server security folder under logins folder :
IIS APPPOOL\DefaultAppPool
phil.o 12-Aug-16 2:57am    
xTMx9 12-Aug-16 11:19am    
i want to make sure i have the correct sql drivers installed. can you check it for me (links are safe ,imagesuploaded to imgur)
here's the files i downloaded from Microsoft's website: link
And here i moved the non thread safe x64 dlls to php ext directory: link
Finally i added them to the extensions in php.ini : link

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!

1 solution

Assuming you have the SQL Server addons installed (per phil.o, above), your SQL query has a problem:

$tsql = "SELECT EmailAddress,Password
         FROM Customer_Detail
         WHERE EmailAddress=$username AND Password=$password ";


Both $username and $password are strings. They must be surrounded by single quotes for SQL:
$tsql = "SELECT EmailAddress,Password
              FROM Customer_Detail
              WHERE EmailAddress='$username' AND Password='$password'";


But, again, make sure you have MS SQL Serve functionality installed. PHP usually comes with MySQL as a standard component (look up LAMP);
 
Share this answer
 
Comments
xTMx9 12-Aug-16 11:16am    
i want to make sure i have the correct sql drivers installed. can you check it for me (links are safe ,imagesuploaded to imgur)
here's the files i downloaded from Microsoft's website: link
And here i moved the non thread safe x64 dlls to php ext directory: link
Finally i added them to the extensions in php.ini : link

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!
W Balboos, GHB 12-Aug-16 11:29am    
https://www.microsoft.com/en-us/download/details.aspx?id=20098 will get you the drivers (from Microsoft, themselves). It's a package and which you need to install depends upon you setup, along with how you'd install them.

My php version is on a Vertrigo server. I put the extension files in the directory and enabled/disabled them until I found those which work for me. Follow their instructions and do online searches for your questions about how to do this with your particular system.

You'll know you have the correct drivers when the SQL queries work. I'd start with a very simple test application to make sure nothing's wrong with the code and only the proper php extensions need to be installed and enabled.
xTMx9 12-Aug-16 11:48am    
that's from where i downloaded the drivers... and followed the "outdated" read me step by step as shown in the pics i uploaded. I'm using x64 architecture for php so i chose those 2 files with x64 andd tried all of them.Non worked.
It's really sh***y that it's 2016 and the process of getting sql server to work with php is still hard and complicated.
Although i have to work on sql server instead of mysql,i think i'll use mysql if i don't figure out the problem by tomorrow.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900