Malik
Malik

Reputation: 103

role based access with foreign key value

I'm a bit new to php and database. I've developed a role based access web application. The scenario is that i have a 2 tables

  1. tbl_user

columns -> user_id, username, school_id (school_id is foreign key)

  1. tbl_school


columns -> school_id, school_name

There are 2 roles only
admin & user
users will be based on schools once the user will be generated, school name ll be associated with the username & user_id

now i can show the id, whether its an admin or user

but once the user is logged in i want the associated school name appears instead of role name.

will be very thankful for the help

INDEX.PHP

        $errors = array(
            1=>"Invalid user name or password, Try again",
            2=>"Please login to access this area"
          );

        $error_id = isset($_GET['err']) ? (int)$_GET['err'] : 0;

        if ($error_id == 1) {
                echo '<p class="text-danger">'.$errors[$error_id].'</p>';
            }elseif ($error_id == 2) {
                echo '<p class="text-danger">'.$errors[$error_id].'</p>';
            }
       ?>
    <form action="authenticate.php" method="post" role="form">
    <div class="form-group">
      <label>UserName :</label>
      <input id="name" name="username" placeholder="username" type="text" class="form-control" required autofocus></div>
      <div class="form-group">
      <label>Password :</label>
      <input id="password" name="password" placeholder="**********" type="password" class="form-control" required></div>

      <input name="submit" type="submit" value=" Login " class="btn btn-primary btn-lg btn-block">
    </form>

AUTHENTICATE.PHP

<?php 
 require 'database-config.php';
 session_start();
 $username = "";
 $password = "";

 if(isset($_POST['username'])){
  $username = $_POST['username'];
 }
 if (isset($_POST['password'])) {
  $password = $_POST['password'];

 } 

 $q = 'SELECT * FROM tbl_user WHERE username=:username AND password=:password';
 $query = $dbh->prepare($q);
 $query->execute(array(':username' => $username, ':password' => $password));

 if($query->rowCount() == 0){
  header('Location: index.php?err=1');
 }else{

  $row = $query->fetch(PDO::FETCH_ASSOC);

  session_regenerate_id();
  $_SESSION['sess_user_id'] = $row['id'];
  $_SESSION['sess_username'] = $row['username'];
        $_SESSION['sess_userrole'] = $row['role'];

        echo $_SESSION['sess_userrole'];
  session_write_close(); 

  $multirole = $row['role'];

    switch ($multirole) {

    case "admin":   
        header('Location: dashboard.php');  
    break;

    case "user":    
        header('Location: user_dashboard.php'); 
    break;

    default:    
    echo "No User Found ! Please Contact Admin";    
    }    
 }? >

DASHBOARD.PHP

<?php 
    session_start();
    $role = $_SESSION['sess_userrole'];
    if(!isset($_SESSION['sess_username']) || $role!="admin"){
      header('Location: index.php?err=2');
    }
? >
html......
Welcome : <?php echo $role; ? >

Upvotes: 1

Views: 260

Answers (1)

TimBrownlaw
TimBrownlaw

Reputation: 5507

Well this is just a quick suggestion based on my reading of your question.

So you can ask your questions of the database... ie Give me the school name that the user_id is in. You would do something like (and this isn't tested code...)

$q = 'SELECT u.username,s.school_name FROM tbl_user u
JOIN tbl_school s ON u.school_id = s.school_id 
WHERE u.user_id=:user_id';

And you just need to give it the correct user_id which you can figure out.

So this is asking, given the user_id ( as you know that already ) Give me the school name in the tbl_school that is related to the school_id in the user table for that user_id.

Does that make sense?

Upvotes: 0

Related Questions