What are some best practices for structuring database entries and using PHP functions to handle data deletion and email notifications?
When deleting data from a database, it's important to ensure that related entries are also deleted to maintain data integrity. Using PHP functions to handle data deletion can help streamline the process and ensure that all necessary steps are taken. Additionally, sending email notifications when data is deleted can provide transparency and accountability in the system.
// Function to delete a record from the database and related entries
function deleteRecord($id) {
// Delete main record
$query = "DELETE FROM table_name WHERE id = $id";
// Delete related entries
$relatedQuery = "DELETE FROM related_table WHERE record_id = $id";
// Execute queries
$result = mysqli_query($connection, $query);
$relatedResult = mysqli_query($connection, $relatedQuery);
// Send email notification
$to = "admin@example.com";
$subject = "Record Deleted";
$message = "The record with ID $id has been deleted.";
$headers = "From: notifications@example.com";
// Send email
mail($to, $subject, $message, $headers);
}