1

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; ? >
Malik
  • 103
  • 6

1 Answers1

0

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?

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28
  • As an aside, it appears that you have added the role in the users table. Which brings me to ask, then why haven't you done the same for the school name? Which should then send off a lightbulb moment where you realize "well while I have created a table for schools with a foreign key in the users table, why haven't I done the same thing for the roles?" And now you should have some idea on how to do that! – TimBrownlaw Oct 28 '16 at 00:02
  • OK, I'm LOST (T_T) what is u & s means in this query? can u tell me about that? – Malik Oct 29 '16 at 18:59
  • The u is an alias for tbl_user and s is an alias for tbl_school just so I do not need to type those out each time. It makes it easier to read. So where you see s.school_id that means tbl_school.school_id. So I Made up u for user and s for school. – TimBrownlaw Oct 30 '16 at 00:09