What are the potential risks of using user input directly in SQL queries when saving dropdown menu selections in a database with PHP?

When using user input directly in SQL queries, there is a risk of SQL injection attacks where malicious code can be injected into the query. To prevent this, it is important to sanitize and validate user input before using it in SQL queries. One way to do this is by using prepared statements with parameterized queries in PHP.

// Assuming $dropdownSelection is the user input from the dropdown menu

// Establish a database connection
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Prepare a SQL statement using a parameterized query
$stmt = $pdo->prepare("INSERT INTO mytable (dropdown_column) VALUES (:dropdownSelection)");

// Bind the dropdown selection to the parameter
$stmt->bindParam(':dropdownSelection', $dropdownSelection);

// Execute the statement
$stmt->execute();