0

I hope you find and doing well, I would like to inform you that I am facing an issue with the created login system, as I'm not able to retrieve current logged in first_name from SQL database; as it's displaying only first record from the database if I logged in with any of the created users.

Here is the created function:

function user_data($user_id) {
    $data = array();
    $user_id = (int)$user_id;

    $func_num_args = func_num_args();
    $func_get_args = func_get_args();

    if ($func_num_args > 1) {
        unset($func_get_args[0]);
        $conn = mysqli_connect('localhost', 'root', '', 'login_system') or die($connect_error);

        $fields = '`' . implode('`, `', $func_get_args) . '`';

$data = mysqli_fetch_assoc(mysqli_query($conn, "SELECT $fields FROM `users` WHERE `user_id` = $user_id"));

    }
    return $data;
}

and here is the code to display the first name:

<div class="widget">
    <h2>Hello, <?php echo $user_data['first_name']; ?>!</h2>
</div>

For example:

1) I have created the following users: alex, billy, Carlo.

2) While login with billy or Carlo usernames; I am only getting the first record details which are related to Alex.

Could you please check and advice if there is an issue in the code and why it's displaying only first record details.

Thank you.

1
  • Are you sure that you are passing billy or Carlo ID ? Commented Dec 1, 2018 at 14:35

1 Answer 1

1

Possibly you are getting wrong user_id as input to query. Also there is a possibility that user_id has duplicates and not marked as unique key.

Please update the question and provide exact table structure with data.

PS: it's very important to escape your parameters before you inject your value to query. Use of pdo is advisable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.