Hello, I'm new here and I have experience in using PHP with MySQL but right now, I need to convert my code so that it can be used to connect to my DB in my SQL server. I have try out the code as follows

db.php

<?php
date_default_timezone_set ("Asia/Kuala_Lumpur");
// $serverName = "CEN302DB-PR1"; //serverName\instanceName
$serverName = "10.34.29.111"; //serverName\instanceName
$connectionInfo = array( "Database"=>"DEV_PDTHSEPM", "UID"=>"admin", "PWD"=>"AdminAdmin1");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
}else{
     die( print_r( sqlsrv_errors(), true));
}

?>

login.php

<?php

    include ('db.php');

    session_start();

     $username = $_POST['username'];
     $password = $_POST['password'];

    $sql = "SELECT * FROM [DEV_PDTHSEPM].[dbo].[USER] WHERE username='$username' AND password ='$password'";

    $stmt = sqlsrv_query($conn, $sql);
    $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
    $count = sqlsrv_num_rows ($stmt);

if($count == 1){

    $_SESSION['user']=array(
    'id' =>$row['id'],
    'username'=>$row['username'],
    'department'=>$row['department'],
    'password'=>$row['password'],
    'role'=>$row['role']
   );

$role=$_SESSION['user']['role'];
   //Redirecting User Based on Role
  ,switch($role){
  case 'user':
  header('location:user/dashboard.php');
  break;
  case 'management':
  header('location:management/index.php');
  break;
  case 'admin':
  header('location:admin/index.php');
  break;
 }
    }
    else
 $error='Your PassWord or UserName is not Found';
 header('location:index.php?error=' . $error);

?>

and here is my index.php (contains the form)

<form method="post" action="login.php">
  <div class="form-group has-feedback">
    <input type="username" name="username" class="form-control" placeholder="Username">
    <span class="glyphicon glyphicon-user form-control-feedback"></span>
  </div>
  <div class="form-group has-feedback">
    <input type="password" name="password" class="form-control" placeholder="Password">
    <span class="glyphicon glyphicon-lock form-control-feedback"></span>
  </div>
  <div class="row">
    <div class="col-xs-8">
    </div>
    <!-- /.col -->
    <div class="col-xs-4">
      <button class="btn btn-primary btn-block btn-flat" type="submit" name="login" value="true">Sign In</button>
    </div>
    <!-- /.col -->

  </div>
</form>

My problem is that, it doesn't direct me to user landing page when I tried login, despite the fact that the username and password is matched in the database. Can someone point out to me what is wrong with my code?

Your code seems to be fine in returning your login data as you mentioned it matches with your db.
This looks more like a file path error, add the following code at the top of each page in php and let us know what errors is returned -

error_reporting(E_ALL);
ini_set('display_errors', 1);

Note that this is for development purposes only and need to be turned off when serving the pages live.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.