How can a dropdown selection be saved in a database using PHP?

To save a dropdown selection in a database using PHP, you can capture the selected value using a form submission and then insert that value into the database using SQL queries. Make sure to sanitize the input to prevent SQL injection attacks.

// Assuming a form with a dropdown selection named 'dropdown'
$selectedValue = $_POST['dropdown'];

// Connect to your database
$connection = new mysqli('localhost', 'username', 'password', 'database');

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

// Sanitize the input
$selectedValue = $connection->real_escape_string($selectedValue);

// Insert the selected value into the database
$sql = "INSERT INTO table_name (column_name) VALUES ('$selectedValue')";
if ($connection->query($sql) === TRUE) {
    echo "Record inserted successfully";
} else {
    echo "Error: " . $sql . "<br>" . $connection->error;
}

// Close the connection
$connection->close();