How can data from one table be kept up-to-date in another table in PHP?

When data from one table needs to be kept up-to-date in another table in PHP, you can achieve this by using SQL queries to update the second table whenever changes are made to the first table. This can be done by setting up triggers in the database to automatically update the second table when changes occur in the first table.

// Assuming we have two tables: table1 and table2

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

// SQL query to update table2 based on changes in table1
$sql = "UPDATE table2
        SET column1 = (SELECT column1 FROM table1 WHERE id = table2.id)
        WHERE id IN (SELECT id FROM table1)";

if ($conn->query($sql) === TRUE) {
    echo "Data updated successfully in table2";
} else {
    echo "Error updating data: " . $conn->error;
}

// Close connection
$conn->close();