drb88830 2014-08-04 11:29
浏览 102

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

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).

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

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:

+--------------+-------------+------------+------+--------------+
| 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!

  • 写回答

1条回答 默认 最新

  • dongtanliefang8765 2014-08-04 12:56
    关注

    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. 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. 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.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度