How can one determine which entry has been selected from a listbox in PHP and use that information to delete the corresponding value from a database?
To determine which entry has been selected from a listbox in PHP, you can use the $_POST superglobal to retrieve the selected value. Once you have the selected value, you can use it to delete the corresponding entry from the database by constructing and executing a DELETE query.
// Retrieve the selected value from the listbox
$selectedValue = $_POST['listbox'];
// Connect to the database
$connection = new mysqli('localhost', 'username', 'password', 'database');
// Check for connection errors
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
// Construct the DELETE query
$query = "DELETE FROM table_name WHERE column_name = '$selectedValue'";
// Execute the DELETE query
if ($connection->query($query) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $connection->error;
}
// Close the database connection
$connection->close();