here if user logged information is correct it should show the main page. in that mainpage right top corner i need to show the username, employee id, employee name. here i can get username correctly. but, i did not get employee id and employee name.
for example : Name - Indian, Username - Indian1990, Emp id - EMP_6504 these all values already stored in database using registration form. now they logged in using Username - Indian1990 and Password - 123456. and then mainpage will open. In that page i need to show EMPLOYEED ID and NAME of the Username - Indian1990. how to fetch the exact details of username in mainpage????

login page php coding :

<?php
    session_start();
    if($_SERVER['REQUEST_METHOD'] == "POST")
    {
    $uname = $_POST['username'];
    $pwd = $_POST['password'];
    $uname = htmlspecialchars($uname);
    $pwd = htmlspecialchars($pwd);

    include("config.php");
    if (!$_POST["username"] || !$_POST["password"]) 
        { 
        echo "You need to provide a username and password."; 
        } 
    $result = mysql_query( "SELECT * FROM emp_register WHERE emp_username = '$uname' AND emp_password = '$pwd'" );
    while($row = mysql_fetch_array($result))
    {
        if($row["emp_username"] == $uname && $row["emp_password"] == $pwd)
        {
        // Login good, create session variables 
         $_SESSION["valid_id"] = $row->id;
         $_SESSION["valid_user"] = $_POST["username"];
         header ('Location: mainpage.php');
        }
        else
        {
         echo "Sorry, could not log you in. Wrong login information.";
        }
    }
    }
?>

mainpage php coding :

before html tag :

<?php
    session_start(); 
    if (!$_SESSION["valid_user"]) 
    { 
    // User not logged in, redirect to login page 
    header("Location: login.php"); 
    }
?>

inside html tag :

<td width="182" rowspan="5" valign="top">
    <?php
        echo "<p class=bluetext>Employee ID : " . $_SESSION["valid_id"]; 
        echo "<p class=bluetext>Username : " . $_SESSION["valid_user"];
        // Display logout link 
        echo "<p class=outtext><a href=\"logout.php\">Logout</a></p>";
    ?>  
</td>

Dani AI

Generated

A few quick, concrete points that will fix this and make the login safer.

The immediate cause is that only the username was being saved into the session, and the fetched DB row was being accessed with the wrong style (object vs array) — good catch by . Make sure you actually select the employee id and name from the database and store those values in session variables after a successful login. Also be sure session_start() runs on every page before you try to read $_SESSION.

A safer, practical pattern (use PDO or mysqli with prepared statements rather than old mysql_*) is: query only the needed columns, verify the password, regenerate the session id, then store emp_id, emp_name, and username in session and redirect. Example (illustrative):

<?php
// assume $pdo is a configured PDO instance
$stmt = $pdo->prepare('SELECT id, emp_id, emp_name, emp_username, emp_password FROM emp_register WHERE emp_username = ?');
$stmt->execute([$username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if ($user && (password_verify($password, $user['emp_password']) || $password === $user['emp_password'])) {
    session_regenerate_id(true);
    $_SESSION['user_id']  = $user['id'];
    $_SESSION['emp_id']   = $user['emp_id'];
    $_SESSION['emp_name'] = $user['emp_name'];
    $_SESSION['username'] = $user['emp_username'];
    header('Location: mainpage.php');
    exit;
}

On the main page, check the session and echo the stored fields (use htmlspecialchars() when outputting). Example:

<?php
session_start();
if (empty($_SESSION['username'])) { header('Location: login.php'); exit; }
echo 'Employee ID: ' . htmlspecialchars($_SESSION['emp_id']);
echo 'Name: ' . htmlspecialchars($_SESSION['emp_name']);

Troubleshooting tips: enable error reporting during development, var_dump($user) right after fetch to confirm column names, ensure column names in the DB match your query, and always exit after header('Location:'). Finally, migrate to hashed passwords (password_hash() / password_verify()) and move off mysql_* as those functions are removed in modern PHP.

It's probably this:

You use while($row = mysql_fetch_array($result)) to fetch your data, so you cannot use $_SESSION["valid_id"] = $row->id; (an object) to access the fetched data. You should use $_SESSION["valid_id"] = $row['id']; (an array), since you are working with an array, not an object.

commented: you are good at code. its working now. thanks. next i need to do upload employee image and then need to fetch. if i have any doubt i will ask you... +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.