How can the selected entry from a pulldown menu be used to delete a corresponding entry from a database in PHP?
To delete a corresponding entry from a database in PHP based on a selected entry from a pulldown menu, you can use a form with a pulldown menu that lists the entries from the database. When the form is submitted, you can retrieve the selected entry using $_POST, and then use this value to construct a DELETE query to remove the corresponding entry from the database.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$selectedEntry = $_POST['selectedEntry'];
// Construct DELETE query
$sql = "DELETE FROM your_table WHERE entry = '$selectedEntry'";
// Execute query
if ($conn->query($sql) === TRUE) {
echo "Entry deleted successfully";
} else {
echo "Error deleting entry: " . $conn->error;
}
}
// Close connection
$conn->close();
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="selectedEntry">
<option value="entry1">Entry 1</option>
<option value="entry2">Entry 2</option>
<option value="entry3">Entry 3</option>
</select>
<input type="submit" value="Delete Entry">
</form>
Keywords
Related Questions
- What are the advantages and disadvantages of using the "container" class in Bootstrap for centering content in PHP websites?
- How can PHP developers ensure that line breaks in CSV files are properly interpreted by Excel during import?
- What is the recommended approach for passing variable content from PHP to JavaScript?