Home > Back-end >  Problem showing a specific variable with $_POST
Problem showing a specific variable with $_POST

Time:11-23

i am trying to store the value of a variable in page 1 and then show it in page 2. The values that i want to store are in a database. This is the database.

Database

This is the code that i am using to access the database and show both the username and the role. My problem is that i can't access the "role" variable.

if(isset($_POST['login_user'])) {


    
    $username = mysqli_real_escape_string($db,$_POST['username']);
    $password = mysqli_real_escape_string($db,$_POST['password']);


    if(empty($username)){
        array_push($errors,"Username is required");
    }

    if(empty($password)){
        array_push($errors, "Password is required");
    }

    if(count($errors) == 0){

        //$password = password_hash($password,PASSWORD_DEFAULT);
        $query = "SELECT * FROM usertable WHERE username='$username' AND password='$password'";

        $results = mysqli_query($db,$query);

        if(mysqli_num_rows($results)){
      
            $_SESSION['username'] = $username;
            $_SESSION['role'] = $_POST['role']; // This doesn't show anything              
            $_SESSION['success'] = "Logged in successfully";
           
            header("location:home.php");
        }
        else{
        
            array_push($errors, "Wrong username and password combination. Please try again.");
           
        }
     }
   
}

I can show the username in page 2 but role is blank.

I tried changing role with password and it works. i have also tried to make a query specific for role but it didnt work.

CodePudding user response:

If you're taking the role value from the database you should get it from the resulting array, not $_POST.

if(mysqli_num_rows($results)){
     $result = mysqli_fetch_assoc($results);
    $_SESSION['username'] = $username;
    $_SESSION['role'] = $result['role'];             
    $_SESSION['success'] = "Logged in successfully";
           
    header("location:home.php");
}
  • Related