简体   繁体   English

PHP / MySQL:同步同一表中的两个字段

[英]PHP / MySQL: synchronize two fields in the same table

I need to synchronize two different booking calendars, beacause both calendars book the same room (or the same event). 我需要同步两个不同的预订日历,因为两个日历都预订同一房间(或同一事件)。

So, if a client book a day (and hours) in calendar_01, this value (booked day and hours) will be automatically updated in calendars_02 (and vice versa). 因此,如果客户在calendar_01中预定一天(和小时),则该值(预定日期和小时)将在calendars_02中自动更新(反之亦然)。

It's important to update (and rewrite the new value) in order of the last time (most recent booking) without a continuous loop. 重要的是,在没有连续循环的情况下,按上次(最新预订)的顺序更新(并重写新值)。

MySql DB MySQL数据库

I'm using a plugin for this and in "calendars" database there is a table called "days", in this table I can see this: 我为此使用了一个插件,并且在“日历”数据库中有一个名为“天”的表,在此表中,我可以看到以下内容:

+--------------+-------------+------------+------+----------------------------------------------------------+
| unique_key   | calendar_id | day        | year | data                                                     |
+--------------+-------------+------------+------+----------------------------------------------------------+
| 1_2014-08-20 | 1           | 2014-08-20 | 2014 | available h10-12; booked h12-14; in pending h14-16;      |
| 2_2014-08-20 | 2           | 2014-08-20 | 2014 | available h 10 - 12; available h12-14; available h14-16; |
| 1_2014-08-21 | 1           | 2014-08-21 | 2014 | available h10-12; available-14; available h14-16;        |
| 2_2014-08-21 | 2           | 2014-08-21 | 2014 | booked h10-12; booked h12-14; in pending h14-16;         |
+--------------+-------------+------------+------+----------------------------------------------------------+

Simplification: column "data" contains the values (TEXT type) that record every rebooking, so: 简化:“数据”列包含记录每次重新预订的值(TEXT类型),因此:

+--------------+-------------+------------+------+--------------+
| unique_key   | calendar_id | day        | year | data         |
+--------------+-------------+------------+------+--------------+
| 1_2014-08-20 | 1           | 2014-08-20 | 2014 | text value A |
| 2_2014-08-20 | 2           | 2014-08-20 | 2014 | text value B |
| 1_2014-08-21 | 1           | 2014-08-21 | 2014 | text value C |
| 2_2014-08-21 | 2           | 2014-08-21 | 2014 | text value D |
+--------------+-------------+------------+------+--------------+

I need to update the values of the same column "data", like this: 我需要更新同一列“数据”的值,如下所示:

+--------------+-------------+------------+------+--------------+
| unique_key   | calendar_id | day        | year | data         |
+--------------+-------------+------------+------+--------------+
| 1_2014-08-20 | 1           | 2014-08-20 | 2014 | text value A |
| 2_2014-08-20 | 2           | 2014-08-20 | 2014 | text value A |
| 1_2014-08-21 | 1           | 2014-08-21 | 2014 | text value D |
| 2_2014-08-21 | 2           | 2014-08-21 | 2014 | text value D |
| 1_2014-08-22 | 1           | 2014-08-22 | 2014 | text value X |
| 2_2014-08-22 | 2           | 2014-08-22 | 2014 | text value X |
| 1_2014-08-23 | 1           | 2014-08-23 | 2014 | text value Y |
| 2_2014-08-23 | 2           | 2014-08-23 | 2014 | text value Y |
+--------------+-------------+------------+------+--------------+

Many thanks in advance for any help! 在此先感谢您的帮助!

I would strongly advice against syncing/Data Replication. 我强烈建议不要进行同步/数据复制。 You'd need to run a deamon 24/7, the risk of running into issues is much higher, it's also less eficient since It has to keep checking for new data in both tables which also means a delay for people to see their new bookings on the site. 您需要运行24/7守护进程,遇到问题的风险更高,效率也更低,因为它必须不断检查两个表中的新数据,这也意味着人们不得不延迟查看新预订在网站上。 And it not so easy to debug when something does go wrong with the deamon. 而且,当恶魔出问题时,调试起来并不容易。

The following solution is much easier to debug, more efficient. 以下解决方案更易于调试,效率更高。 I would suggest you write abstract CRUD code for the data: Create, Read, Update and Delete. 我建议您为数据编写抽象的CRUD代码:创建,读取,更新和删除。 Create and Update are probably the ones you're most interested in, what you would do is something like this: 创建和更新可能是您最感兴趣的,您将执行以下操作:

<?php
function create($id, $data)
{
    $id = mysql_real_escape_string($id);
    $data = mysql_real_escape_string($data['data']);
    mysqL_query("INSERT INTO calendars (unique_key,data) VALUES('".$id."','".$data."')");
    mysqL_query("INSERT INTO days (unique_key,data) VALUES('".$id."','".$data."')");
}
function update($id, $data)
{
    $id = mysql_real_escape_string($id);
    $data = mysql_real_escape_string($data['data']);
    mysqL_query("UPDATE calendars SET data = '".$data."' WHERE unique_key = '".$id."'");
    mysqL_query("UPDATE days SET data = '".$data."' WHERE unique_key = '".$id."'");
}
create('1_2014-08-20', array(
    'data' => 'data here'
));
update('1_2014-08-20', array(
    'data' => 'data here'
));

This is as simple as passing data into it. 这就像将数据传递到其中一样简单。 If you ever modify the SQL structure you can create a new abstraction set of functions/classes that follows the new database structure and it's as easy as swapping out an include. 如果您曾经修改过SQL结构,则可以创建遵循新数据库结构的新的函数/类抽象集,就像交换一个include一样容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM