Mysql分布式之原生分表(改)03

思路:

先查询主表所有的数据
再根据编辑传递的id取模后更新对应的分表数据,这里主表数据并未改变
最后将更新总表sql应该插入redis消息队列,通过异步定时任务更新总表数据

实现代码

先查询主表所有的数据,findAll.php

<?php

require './RunDbPdo.php';
$model = new RunDbPdo();
$model->configFile = './config/user.config.php';
$sql = "select * from mm_user";
$data = $model->getRows($sql);
?>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="utf-8">
    <title>用户管理系统</title>
    <link href="./css/property-system.css" rel="stylesheet">
    <link href="./css/operate-system.css" rel="stylesheet">
        </head>
        <body>
            <main>
                <section class="content">
                    <header class="content-header">查看用户</header>
                    <div class="box">
                    <table class="tab-list-1 tab-list-break">
                        <colgroup>
                            <col style="width:auto">
                            <col style="width:auto">
                            <col style="width:auto">
                            <col style="width:auto">
                            <col style="width:auto">
                            <col style="width:auto">
                            <col style="width:auto">
                            <col style="width:auto">
                        </colgroup>
                        <thead>
                        <tr>
                            <th>用户ID</th>
                            <th>用户名</th>
                            <th>年龄</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                            <?php foreach($data as $key=>$value){ ?>
                        <tr>
                            <td><?=$value['user_id']?></td>
                            <td><?=$value['username']?></td>
                            <td><?=$value['age']?></td>
  
                            <td>
				<a href="edit.php?user_id=<?=$value['user_id']?>">编辑</a>
			    </td>
                        </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
            </div>
                </section>
            </main>
    </body>
</html>

edit.php
再根据编辑传递的id取模后更新对应的分表数据,这里主表数据并未改变,需要异步处理主表与分表同步数据问题

<?php
require './RunDbPdo.php';
$model = new RunDbPdo();
$model->configFile = './config/user.config.php';
if (isset($_GET) && !empty($_GET)) {
    extract($_GET);
    $user_id = isset($user_id) ? $user_id : '';
    $sql = "select * from mm_user where user_id='{$user_id}'";
    $data = $model->getRow($sql);
}

if (isset($_POST) && !empty($_POST)) {
    extract($_POST);
    $user_id = isset($user_id) ? $user_id : 0;
    $username = isset($username) ? $username : 0;
    $age = isset($age) ? $age : 0;
    //1、根据id取模后更新分表数据
    $d = $user_id % 2;
    $sql2 = "update mm_user{$d} set username='{$username}',age='{$age}' where user_id='{$user_id}'";
    $res2 = $model->query($sql2);
    //2、这二步将更新总表sql应该插入redis消息队列,通过异步定时任务更新总表数据
    if($res2) {
        $sql = "update mm_user set username='{$username}',age='{$age}' where user_id='{$user_id}'";
        $res = $model->query($sql);
    }
    if($res2){
        header('location:findAll.php');
    }
}

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>用户管理系统</title>
    <link href="./css/property-system.css" rel="stylesheet">
    <link href="./css/operate-system.css" rel="stylesheet">
</head>
<body>
<main>
    <section class="content">
        <fieldset class="form-list-32">
            <h3 class="operate-title-1"><i></i>编辑用户信息</h3>
            <form action="edit.php" id="editFormId" method="post" autocomplete="off">
                <ul>
                    <input type="hidden" name="user_id" value="<?= $data['user_id'] ?>">
                    <li><h6>用户名:</h6>
                        <aside><input type="text" name="username" value="<?= $data['username'] ?>"
                                      class="tbox30 tbox30-6"/></aside>
                    </li>
                    <li><h6>年龄:</h6>
                        <aside><input name="age" value="<?= $data['age'] ?>" maxlength="11" class="tbox30 tbox30-6"
                                      type="text"></aside>
                    </li>
                    <li class="agent-subbtn-wrap mt30px">
                        <h6>&nbsp;</h6>
                        <aside><input class="btn-2" type="submit" value="提交"></aside>
                    </li>
                </ul>

            </form>
        </fieldset>
    </section>
</main>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值