How can PHP sessions be effectively utilized to modify user-specific data in a database?

To modify user-specific data in a database using PHP sessions, you can store the user's unique identifier (such as their user ID) in a session variable. Then, retrieve this identifier from the session when updating the database records for that user.

// Start the session
session_start();

// Store the user's ID in a session variable
$_SESSION['user_id'] = 123;

// Retrieve the user's ID from the session
$user_id = $_SESSION['user_id'];

// Update user-specific data in the database
// Example query using user_id variable:
// UPDATE users SET data = 'new_data' WHERE id = $user_id