title: How to access the username of the user that is logged in
body: <p>I have made a web application. I have completed the registration and login. There are two user types that can register, student or professor.   </p>
  
  <p>I have a session running from the login time until logout. If you login as a user there are certain things you can do. One of them is close an appointment. This can be done from a radiobuton in a loginstudent.php (for example) page and submit button. This leads you to another .php page. On that page I have to use the username of the user that is logged in (in my case that would be the student) for a mysql query. I don't know how to access this.</p>
  
  <pre><code> $sql = "SELECT * FROM appointment WHERE prof_id=(SELECT user_id FROM user WHERE lastname='$prof_last') AND student_id=(SELECT user_id FROM user WHERE username=$username);";
  </code></pre>
  
  <p>I think this is wrong.</p>
  
  <p><strong>edit</strong>
  this is the complete login</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  if (($_POST['submit'])) {
  
      include_once 'dbh.php';
  
      $username = $_POST['username'];
      $password = $_POST['password'];
  
  
      //check if empty
  
      if (empty($username) || empty($password)) {
          header("Location: http://localhost/TexnologiaLogismikou/index.php?login=empty");
          die;
          exit();
      } else {
          $sql = "SELECT * FROM user WHERE username='$username';";
          $result = mysqli_query($conn, $sql);
          $resultCheck = mysqli_num_rows($result); // tsekarei posa vrethikan
          if ($resultCheck &lt; 1) {
              header("Location: http://localhost/TexnologiaLogismikou/index.php?login=error");
              die;
              exit();
          } else {
  
              if ($row = mysqli_fetch_assoc($result)) {
                  $hash_password_check = password_verify($password, $row['password']);
                  if ($hash_password_check == false) {
                      header("Location: http://localhost/TexnologiaLogismikou/index.php?login=error");
                      die;
                      exit();
                  } elseif ($hash_password_check == true) {
                      //login
  
                      if ($user_type=="student") {
  
  
                          $_SESSION['username'] = $row['username'];
                          $_SESSION['firstname'] = $row['firstname'];
                          $_SESSION['lastname'] = $row['lastname'];
                          $_SESSION['user_type'] = $row['user_type'];
                          header("Location: http://localhost/TexnologiaLogismikou/student.php?login=success");
                          die;
                          exit();
                      } else {
  
                          $_SESSION['username'] = $row['username'];
                          $_SESSION['firstname'] = $row['firstname'];
                          $_SESSION['lastname'] = $row['lastname'];
                          $_SESSION['user_type'] = $row['user_type'];
                          header("Location: http://localhost/TexnologiaLogismikou/professor.php?login=success");
                          die;
                          exit();
                      }
                  }
              }
          }
      }
  } else {
      header("Location: http://localhost/TexnologiaLogismikou/index.php?login=error");
      die;
      exit();
  }
  </code></pre>
  
  <p>then goes</p>
  
  <pre><code>&lt;?php
  include_once 'header.php';
  ?&gt;
  
  &lt;script&gt;
  
      $(document).ready(function () {
          $('#5').hide();
  
          $("form input:radio").change(function () {
              if ($(this).val() === "appointment") {
                  $("#5").show();
  
  
              } else {
                  $("#5").hide();
  
              }
          });
  
  
  
  
  
      });
  
  &lt;/script&gt;
  
  
  
  
  
  
  &lt;section class="main-container"&gt;
      &lt;a href="footer.php"&gt;&lt;/a&gt;
      &lt;a href="dbh.php"&gt;&lt;/a&gt;
      &lt;a href="header.php"&gt;&lt;/a&gt;
  &lt;div class="main-wrapper"&gt;
      &lt;form class="student-form" action="studentphp.php" method="POST"&gt;
          &lt;link href="style.css" rel="stylesheet" type="text/css"/&gt;
          &lt;h4 id="9"&gt;Select your action:&lt;/h4&gt;&lt;br&gt;
  
          &lt;input type="radio" name="action" value="appointment"&gt;
          &lt;p id="8"&gt;Show your Appointments&lt;/p&gt;&lt;br&gt;
          &lt;input id="5" type="text" name="prof_last" placeholder="Professor Lastname"&gt;
          &lt;input id="6" type="radio" name="action" value="upload"&gt;
          &lt;p id="7"&gt;Upload a File&lt;/p&gt;&lt;br&gt;
  
          &lt;input type="submit" name="submit"&gt;
      &lt;/form&gt;
  
  &lt;/div&gt;
  &lt;/section&gt;
  
  
  
  &lt;?php
  include_once 'footer.php';
  ?&gt;
  </code></pre>
  
  <p>and this is the page i need the username </p>
  
  <pre><code>&lt;?php
  
  include_once 'header.php';
  
  
  if (($_POST['submit'])) {
      include_once 'dbh.php';
  
          $prof_last = $_POST['prof_last'];
  
      if (empty($prof_last)) {
          header("Location: http://localhost/TexnologiaLogismikou/student.php?professorlastname=empty");
          die;
          exit();
      } else {
          $sql = "SELECT * FROM appointment WHERE prof_id=(SELECT user_id FROM user WHERE lastname='$prof_last') AND student_id=(SELECT user_id FROM user WHERE username=$username);";
          mysqli_query($conn, $sql);
  
  
      }
  } else {
      header("Location: http://localhost/TexnologiaLogismikou/student.php"); //se ksanapaei sto sign up
      die;
      exit();
  }
  ?&gt;
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  &lt;?php
  
  include_once 'footer.php';
  </code></pre>