I am developing a inventory management web application using php and mysql. This application will runs on different locations. while any one place disconnect the internet at the time data will be land in local database and when active internet that data automatically synchronizing the remote database. is there way pls let me know.

thanks and advance.

Dani AI

Generated

A practical offline-first pattern for PHP/MySQL is to write locally to your normal tables and also append each change to an outbox table with a monotonic updated_at, a globally unique id (UUID), and an origin_site value. A background job checks connectivity and POSTs unsent outbox rows (batched JSON) to a remote /sync endpoint; on HTTP failure, it leaves them queued for retry. Use PHP cURL for the POST, and wrap dequeue/mark-sent in a transaction so you never lose or double-send entries if the process dies midway. See PHP’s cURL docs for options, return handling, and timeouts. PHP cURL manual

Example local sender (simplified):

$rows = $pdo->query("SELECT * FROM outbox WHERE sent_at IS NULL ORDER BY id LIMIT 100")->fetchAll(PDO::FETCH_ASSOC);
$ch = curl_init('https://example.com/sync');
curl_setopt_array($ch, [
  CURLOPT_POST => true,
  CURLOPT_POSTFIELDS => json_encode($rows),
  CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 10,
]);
$response = curl_exec($ch);
if ($response !== false && curl_getinfo($ch, CURLINFO_RESPONSE_CODE) === 200) {
  // mark rows as sent inside a DB transaction
}
curl_close($ch);

On the remote side, upsert idempotently and resolve conflicts by timestamp (last-write-wins shown here). MySQL supports this safely with INSERT ... ON DUPLICATE KEY UPDATE; you can reference the incoming row via an alias in modern MySQL: MySQL INSERT ... ON DUPLICATE KEY UPDATE

INSERT INTO items (id, name, updated_at, origin_site)
VALUES (?, ?, ?, ?) AS new
ON DUPLICATE KEY UPDATE
  name = IF(new.updated_at > updated_at, new.name, name),
  updated_at = GREATEST(updated_at, new.updated_at);

Use transactions for dequeue/send/ack cycles to keep state consistent during retries. MySQL START TRANSACTION, COMMIT, and ROLLBACK

Recommended Answers

All 7 Replies

This application will runs on different locations

What language is this application written in?

any one place disconnect the internet at the time data will be land in local database and when active internet that data automatically synchronizing the remote database

What are the rules for syncing? Have you thought about that?

Hi pritaeas, am using PHP/MYSQL.

Hello, we will need more info.
What kind of data are you saving etc.

I had developed such type of solutions few months ago.

For the sync you can create the server side code which will get the data and the local server end which will put the data.

Now at local end you will mark a time and after that time you can get the data in json format of SQL query. Now send this data to remote server and put/sync this information on the remote server.

From local server you will need to havr curl as with this you can call the remote script after putting the data by any way like FTP from php.

Good luck.

Use apps like Microsoft's SyncToy, SyncBack etc

Keeping a que of the changes made and just submitting them to the main database once connection is re-established really is your best bet here.

code please

commented: really? -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.