How can PHP developers securely handle passing multiple values from a select element to a PHP script for database insertion?

When passing multiple values from a select element to a PHP script for database insertion, it is important to sanitize and validate the input to prevent SQL injection attacks. One way to securely handle this is by using prepared statements with parameterized queries in PHP. This allows for the separation of SQL logic from user input, reducing the risk of SQL injection.

// Assume $conn is the database connection object

// Get the selected values from the select element
$selectedValues = $_POST['selectedValues'];

// Prepare a SQL statement with a parameterized query
$stmt = $conn->prepare("INSERT INTO table_name (column_name) VALUES (?)");

// Bind the selected values to the parameter
$stmt->bind_param("s", $selectedValues);

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

// Close the statement and database connection
$stmt->close();
$conn->close();