When allowing users to change website design preferences, such as through a listbox, what are the best practices for storing and retrieving this information for future visits?

When allowing users to change website design preferences, such as through a listbox, it is important to store this information in a persistent manner so that it can be retrieved for future visits. One common approach is to store the user's preferences in a database table, associating them with the user's unique identifier. This way, when the user logs in or returns to the website, their preferences can be retrieved and applied accordingly.

// Assuming user preferences are stored in a database table called 'user_preferences'
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve user preferences
$user_id = $_SESSION['user_id']; // Assuming user is logged in and user_id is stored in session

$sql = "SELECT design_preference FROM user_preferences WHERE user_id = $user_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        $design_preference = $row['design_preference'];
    }
} else {
    $design_preference = "default"; // Default design preference
}

// Apply design preference to website
echo "<link rel='stylesheet' href='styles/$design_preference.css'>";

// Close database connection
$conn->close();