What are some best practices for handling dropdown selection of variables like "Jahr" in PHP?

When handling dropdown selection of variables like "Jahr" in PHP, it is important to properly sanitize and validate user input to prevent security risks such as SQL injection attacks. One best practice is to use prepared statements when querying the database to avoid direct user input in SQL queries. Additionally, using server-side validation to ensure that only valid options are selected can help improve the overall user experience.

// Sample PHP code snippet for handling dropdown selection of "Jahr"

// Assuming $jahrOptions is an array of valid options for "Jahr"
$jahrOptions = array(2021, 2022, 2023, 2024);

// Sanitize and validate user input
if(isset($_POST['jahr']) && in_array($_POST['jahr'], $jahrOptions)) {
    $selectedJahr = $_POST['jahr'];
    // Use $selectedJahr in your application logic
} else {
    // Handle invalid input or display an error message
}