How can the res() function be modified to delete a database entry instead of inserting one?
To modify the res() function to delete a database entry instead of inserting one, we need to change the SQL query from an INSERT statement to a DELETE statement. We also need to pass the appropriate parameters to identify the specific entry to be deleted.
function res($id) {
$conn = new mysqli("localhost", "username", "password", "database");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM table_name WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
if ($stmt->execute()) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$stmt->close();
$conn->close();
}
Related Questions
- What is the difference between using fopen and file_get_contents in PHP to read a file?
- What role does the use of iframes play in the PHP code provided and how does it relate to the issue of modifying header information?
- What measures can be taken to prevent Man-in-the-Middle attacks when storing passwords online in PHP?