PHP display name of login user

I am trying to display the name of a user when they are logged in. My code uses $_SESSIONS to store the name, but since there no input in my login in page, the name doesn’t get assign and it ends up being just hello, instead of something like hello, John Smith.
I’ve tried using sql to select the name by matching the email to the email of the logged in user, and storing that in $_SESSION but it still doesn’t print name of user.

my server.php

<?php
include_once "inc/user-connection.php";

session_start();

$name = mysqli_real_escape_string($conn, $_POST['name']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$username = mysqli_real_escape_string($conn, $_POST['username']);

if (isset($_POST['admin-sign-in'])) {
    if (!empty($email)) {
        if (!empty($password)) {
           
            $sql = 'SELECT email, password FROM admin WHERE email = ?';

            // preparing the SQL statement
            if ($stmt = $conn->prepare($sql)) {
                $stmt->bind_param('s', $_POST['email']);
                $stmt->execute();
                $stmt->store_result(); // Store the result so we can check if the account exists in the database.

                // If email exists in sign_up table
                if ($stmt->num_rows > 0) {
                    $stmt->bind_result($email, $password);
                    $stmt->fetch();

                    // if password user enters matches the one in the database
                    if (password_verify($password, $hashed_password)) {
                        $query = mysqli_query($conn, $sql);
                        $row = mysqli_fetch_array($query);
                        $_SESSION['name'] = $row['name'];

                        // upon successful login, redirect user to landing apge
                        header("location: dashboard.php");
                        die();
                    } else {
                        // Incorrect password
                        header("location: ../html/sign-in-error.html");
                        die();
                    }
                } else {
                    // Incorrect username
                    header("location: ../html/sign-in-error.html");
                    die();
                }
                $stmt->close();
            }
        } else {
            header("location: ../html/404-error.html");
            die();
        }
    } else {
        header("location: ../html/404-error.html");
        die();
    }
}

my dashboard.php

<?php
session_start();
?>
<div class="d-block">
                    <h1 class="lead fw-normal text-muted mb-4 px-lg-10">Hello,

                        <?php
                        echo $_SESSION['name'];
                        ?>
                    </h1>
</div>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You did not select the name and you are fetching it.

$sql = 'SELECT email, password, name FROM admin WHERE email = ?';

or

$sql = 'SELECT * FROM admin WHERE email = ?';

should fix the issue.

Additional: you can remove all your else statements since all of it will give the same result.

<?php

include_once "inc/user-connection.php";

session_start();

$name = $_POST['name'];
$password = $_POST['password'];
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$email = $_POST['email'];
$username = $_POST['username'];

if (isset($_POST['admin-sign-in'])) {
    if (!empty($email)) {
        if (!empty($password)) {
            $sql = 'SELECT * FROM admin WHERE email = ?';

            // preparing the SQL statement
            if ($stmt = $conn->prepare($sql)) {
                $stmt->bind_param('s', $_POST['email']);
                $stmt->execute();
                $stmt->store_result(); // Store the result so we can check if the account exists in the database.

                // If email exists in sign_up table
                if ($stmt->num_rows > 0) {
                    $stmt->bind_result($email, $password, $name);
                    $stmt->fetch();

                    // if password user enters matches the one in the database
                    if (password_verify($password, $hashed_password)) {
                        $query = mysqli_query($conn, $sql);
                        $row = mysqli_fetch_array($query);
                        $_SESSION['name'] = $row['name'];

                        // upon successful login, redirect user to landing apge
                        header("location: dashboard.php");
                        die();
                    }
                }
                $stmt->close();
            }
        }
    }
    header("location: ../html/404-error.html");
    die();
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x