<?php
// Local database configuration
$localHost = 'localhost';
$localDbname = 'pharmacy_management';
$localUsername = 'root';
$localPassword = '';

// Online database configuration
$onlineHost = 'sql105.infinityfree.com';
$onlineDbname = 'if0_37633323_pharmacy_db';
$onlineUsername = 'if0_37633323';
$onlinePassword = 'CwcMg9lVTGg';

try {
    // Connect to local database
    $localPdo = new PDO("mysql:host=$localHost;dbname=$localDbname", $localUsername, $localPassword);

    // Connect to online database
    $onlinePdo = new PDO("mysql:host=$onlineHost;dbname=$onlineDbname", $onlineUsername, $onlinePassword);

    // List of tables to copy data from
    $tables = ['drugs', 'owings', 'log_history']; // Replace with your actual table names

    foreach ($tables as $table) {
        // Fetch data from the local table
        $data = $localPdo->query("SELECT * FROM $table")->fetchAll(PDO::FETCH_ASSOC);

        // Insert data into the online table
        foreach ($data as $row) {
            $columns = implode(", ", array_keys($row));
            $values = implode(", ", array_map(fn($val) => $onlinePdo->quote($val), $row));
            $onlinePdo->exec("INSERT INTO $table ($columns) VALUES ($values)");
        }
    }

    echo "Data sync completed successfully!";
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
?>
