how to sync local mysql database and live server database? My plan is to store data in my local MySQL database and after a time interval it will update my live MySQL database remotely. Is it possible?

I'm planing a inventory management script in PHP MySQL.  The real problem is in our country net connection drops suddenly. So I want to first write to the local MySQL database and then it will update the real server.  In any situation like internet connection dropped the script will work locally to gather data and retrieve data. When the internet connection comes back it will update the data to the real server. I want to protect against internet connection failures.

Nothing will update the real server in the mean time because the users are only in the office where the internet connection will drop.  No one from the outside will able to update the database. Only the information can be retrieved from the live server to the shop's owner.
Hello you can sync your local database data with your live mysql server. I had done it making a local sync file that will present all the tables of the local database that I wanted to update to live database and then calling a call back function in footer file that function calls the function to sync local data to live server

Footer.php

    jQuery(document).ready(function($){
    working = false;
    var do_sync = function(){
        if ( working ) { return; }
        working = true;
        jQuery.post(
            "&lt;?php echo $this-&gt;config-&gt;item("sync_url"); ?&gt;", 
            {},
            function(ret){
                working = false;
            }
        );
    }
    window.setInterval(do_sync, 10000);
});


Config.php

$config['sync_server_url'] = "http://livesite.com/index.php/sync_server";
$config['sync_url'] = "http://localhost/localsite/index.php/sync";


sync.php contents Also you need to on CURL extension in local site wamp so that it can communicate with the server side, If database record that is already synced to server edited then sync field in DB should be set to 0 so that on edit it can overwrite server DB again.

Sync.php

&lt;?php
    class Sync extends CI_Controller {
        private $tables;
        function __construct() {
            parent::__construct();
            $this-&gt;load-&gt;database();
            $this-&gt;tables = array(
                "customers" =&gt; array("per_id"),
                "db_log" =&gt; array("id"),
                "inventory" =&gt; array("tr_id"),
                "material" =&gt; array("id_mat"),
                "mtracking" =&gt; array("id_tra"),
                "orecipie" =&gt; array("id_rec"),
                "moutlets" =&gt; array("id_st"),
                "opeople" =&gt; array("per_id"),
                "oreceivings" =&gt; array("rec_id"),
                "osales" =&gt; array("sal_id"),
                "osales_items" =&gt; array("sal_id", "itm_id"),
                "osales_payments" =&gt; array("sal_id"),
                "osales_sus" =&gt; array("sal_id", "emp_id"),
                "osales_sus_items" =&gt; array("sal_id", "itm_id"),
                "osales_sus_payments" =&gt; array("sal_id"),
                "osuppliers" =&gt; array("per_id"),
            );


            $this-&gt;id_store = $this-&gt;config-&gt;item('id_store');


        }

        function index() {
            echo '&lt;br&gt;'.date("H:i:s A");

            $newLine = "\r\n";
            $output = $update = "";

            foreach($this-&gt;tables as $table_name =&gt; $columns ){

                $col_check = "SHOW COLUMNS FROM `{$table_name}` LIKE 'is_sync'";
                $col_exists = $this-&gt;db-&gt;query($col_check);
                if ( $col_exists-&gt;num_rows &gt; 0 ) { // is_sync column exists for this table 
                    $sql = "SELECT * FROM {$table_name} WHERE is_sync = 0 LIMIT 500" ;
                    $rows = $this-&gt;db-&gt;query($sql);
                    if ( $rows-&gt;num_rows &gt; 0 ) { // where is_sync = 0
                        foreach ( $rows-&gt;result_array() as $row ) {
                            $col_val = $update_col_val = $already_exists = array();
                            foreach($row as $name =&gt; $val) {
                                if ( is_null( $val ) ) {
                                    continue;
                                }
                                $val = mysql_real_escape_string($val);

                                $update_col_val[] = " `{$name}` = '{$val}' ";

                                if ( "is_sync" === $name ) {
                                    $val = 1;
                                }

                                if ( "id_store" === $name ) {
                                    $val = $this-&gt;id_store;
                                }

                                $col_val[] = " `{$name}` = '{$val}' ";

                                if ( $name === $columns[0] ){
                                    $already_exists[] = " `$columns[0]` = '{$val}'";
                                }

                                if ( $name === $columns[1] ){
                                    $already_exists[] = " `$columns[1]` = '{$val}'";
                                }


                            }

                            if ( is_array($col_val) &amp;&amp; count($col_val) &gt; 0 ) {

                                //echo $this-&gt;table_keys[$table_name];
                                $output .= "SELECT * FROM `{$table_name}` WHERE `id_store` = {$this-&gt;id_store} AND " . implode(" AND ", $already_exists) . " ||";
                                $output .= "INSERT INTO `{$table_name}` SET " . implode(",", $col_val) . " ||";
                                $output .= "UPDATE `{$table_name}` SET " . implode(",", $col_val) . " WHERE `id_store` = {$this-&gt;id_store} AND " . implode(" AND ", $already_exists);
                                $output .= " ;; ";

                                $update .= "UPDATE {$table_name} SET is_sync = 1 WHERE " . implode(" AND ", $update_col_val) . " ;; ";
                            }
                        }
                    } // if is_sync = 0

                } // if is_sync column exists 
            }

            if ( !empty($output) &amp;&amp; $this-&gt;post_data($output) ) {
                $update = explode(" ;; ", $update);
                foreach($update as $upq){
                    if (!empty($upq) ) {
                        $this-&gt;db-&gt;query($upq);
                    }
                }
            } else {
                echo $result;
            }

            echo date("H:i:s A");

        }

        function post_data($qry){
            $qry = htmlspecialchars(urlencode($qry));
            $data = "qry=" . $qry;
            $ch = curl_init();
            curl_setopt( $ch, CURLOPT_URL,$this-&gt;config-&gt;item("sync_server_url") );
            curl_setopt( $ch, CURLOPT_AUTOREFERER, 1);
            curl_setopt ( $ch, CURLOPT_HTTPHEADER, array ( 'Content-length: ' . strlen($qry) ) );
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt( $ch, CURLOPT_POST,1);
            curl_setopt( $ch, CURLOPT_POSTFIELDS,$data);
            curl_setopt( $ch, CURLOPT_CRLF, 1);
            $result = curl_exec($ch);
            curl_close($ch);
            if ( 1 === intval($result) ) {
                return TRUE;
            } else {
                echo $result;
                return $result;
            }
        }


    }
?&gt;


Then after that you need to make a controller on live server I named it sync_server.php

sync_server.php

&lt;?php
    class Sync_server extends CI_Controller {
        function __construct() {
            parent::__construct();
            $this-&gt;load-&gt;database();    
        }

        function index() {
            if (isset( $_POST['qry'] ) ) {
                $qry = htmlspecialchars_decode( urldecode( $_POST['qry']));


                //$this-&gt;connect_local_db();

                $qry = explode(" ;; ", $qry);

                foreach($qry as $q) {

                    $q = explode( "||", $q );

                    //print_r($q);

                    $exists = mysql_query( $q[0] ) or die ("&lt;hr/&gt;" . mysql_error() ."&lt;br/&gt;" . $q[0]);

                    if ( $exists &amp;&amp; mysql_num_rows( $exists ) ) {
                        mysql_query( $q[2] ) or die ("&lt;hr/&gt;" . mysql_error()."&lt;br /&gt;".$q[2]);
                    } else{
                        mysql_query( $q[1] ) or die ("&lt;hr/&gt;" . mysql_error()."&lt;br /&gt;".$q[1]);
                    }

                    //$this-&gt;db-&gt;query($q);
                }
                die("1");
            } else {
                echo "qry not found";
            }
        }


    }
?&gt;