What could be a possible solution to ensure that only the data for the logged-in user is displayed?

To ensure that only the data for the logged-in user is displayed, you can use session variables to store the user's ID or username upon login. Then, when fetching data from the database, include a condition in the query to only retrieve records that belong to the logged-in user. This way, unauthorized users won't be able to access data that doesn't belong to them.

<?php
session_start();

// Check if user is logged in
if(isset($_SESSION['user_id'])) {
    $user_id = $_SESSION['user_id'];

    // Connect to database and retrieve data for the logged-in user
    $query = "SELECT * FROM user_data WHERE user_id = $user_id";
    // Execute query and display data
} else {
    // Redirect to login page or display an error message
}
?>