PHP Pulling info from the DB based on the logged in user.

Hello everyone,

I am complete newbie to php/MySQL. I have been working on a class project in designing a portal website in which when a registered user logs in (index.php), he is redirected to the main page (dashboard.php) which then will display his demographic information that is pulled from the DB.

I don’t know how to code the variable that will capture the logged in user and passed to the other page so MySQL code would just reflect the current logged in user. This way anyway who logs in will have one MySQL code in the main page but will have their personal information displayed.

Below are the codes of my index page

<?php require_once('Connections/connection.php'); ?> <?php require_once('Connections/function.php'); ?> <?php // establish session if (!isset($_SESSION)) { session_start(); } // previous page $_SESSION['PrevPage'] = "index.php"; ?> <?php //*******************************// // Log In //*******************************// // index.php?logout=true $logout = $_SERVER['PHP_SELF'] . "?logout=true"; if ((isset($_GET['logout'])) &&($_GET['logout']=="true")) { // clear old session $_SESSION['Username'] = NULL; $_SESSION['UserGroup'] = NULL; $_SESSION['PrevUrl'] = NULL; unset($_SESSION['Username']); unset($_SESSION['UserGroup']); unset($_SESSION['PrevUrl']); // index.php header("Location: index.php"); } ?> <?php //*******************************// // Log In //*******************************// // login_form.php $connection = new mysqli($hn, $un,$pw,$db); $_SESSION['login_form_title'] = "Log In"; // member login if (isset($_POST['username']) && isset($_POST['password'])) { // credential $username = $_POST['username']; $password = $_POST['password']; // database connection if ($connection->connect_error) die($connection->connect_error); // existing member? $query = sprintf("SELECT username, password, userlevel FROM member WHERE username=%s AND password=%s", GetSQLValue($username, "text"), GetSQLValue($password, "text")); // validation $result = $connection->query($query); if ($result) { // $totalRows = ($result->num_rows); if ($totalRows=1) { // session created $_SESSION['Username'] = $username; $_SESSION['UserGroup'] = mysql_result($result, 0, 'userlevel'); // main.php header("Location: main.php"); } else { //try again: login_form.php header("Location: login_form.php"); } } else { //try again: login_form.php header("Location: login_form.php"); } } That would then redirect me to the main page which currently is only html. I would like to start writing queries but I am not sure how to capture the username variable that has been logged in. My professor is not very helpful and we only had 3 lectures on php programming. ::A Patient Portol::
Welcome to A Patient Portol
Please help.

Your code needs some work and has a few issues, but to answer your question, you are already saving the username to a SESSION here:

// session created
$_SESSION[‘Username’] = $username;

You also need to save the user_id to a session as well for further queries and actions. As is, if you echo $_SESSION[‘Username’] it will display the username.

[php]echo “Welcome {$_SESSION[‘Username’]}”;[/php]

Depending on how much user information you want to display, you may or may not want to put that data in a session. If it’s just first and last name a session is fine. Otherwise you should just query for the data you want when it is needed.

Sponsor our Newsletter | Privacy Policy | Terms of Service