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


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

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




    include ('db.php');


     $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){

    'id' =>$row['id'],

   //Redirecting User Based on Role
  case 'user':
  case 'management':
  case 'admin':
 $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 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 class="row">
    <div class="col-xs-8">
    <!-- /.col -->
    <div class="col-xs-4">
      <button class="btn btn-primary btn-block btn-flat" type="submit" name="login" value="true">Sign In</button>
    <!-- /.col -->


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 -

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.