How can PHP scripts be structured to automatically update a calendar table based on changes in an event table?

To automatically update a calendar table based on changes in an event table, you can create a PHP script that listens for changes in the event table using triggers in your database. When a change is detected, the script can then update the corresponding entries in the calendar table accordingly.

<?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);
}

// Trigger to update calendar table when event table is updated
$sql = "CREATE TRIGGER update_calendar
AFTER UPDATE ON event
FOR EACH ROW
BEGIN
    UPDATE calendar
    SET event_date = NEW.event_date,
        event_name = NEW.event_name
    WHERE event_id = NEW.event_id;
END";

if ($conn->query($sql) === TRUE) {
    echo "Trigger created successfully";
} else {
    echo "Error creating trigger: " . $conn->error;
}

$conn->close();
?>