How can an admin function be implemented in a PHP script using MySQL for deletion?
To implement an admin function for deletion in a PHP script using MySQL, you can create a form that allows the admin to select the item to be deleted and then execute a MySQL query to delete the selected item from the database.
<?php
// Check if the form is submitted
if(isset($_POST['delete_item'])) {
// Get the selected item ID from the form
$item_id = $_POST['item_id'];
// Connect to the database
$conn = new mysqli('localhost', 'username', 'password', 'database_name');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Execute the MySQL query to delete the selected item
$sql = "DELETE FROM items WHERE id = $item_id";
if ($conn->query($sql) === TRUE) {
echo "Item deleted successfully";
} else {
echo "Error deleting item: " . $conn->error;
}
// Close the database connection
$conn->close();
}
?>
<form method="post">
<label for="item_id">Select item to delete:</label>
<select name="item_id">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
<input type="submit" name="delete_item" value="Delete">
</form>