思路:
先查询主表所有的数据
再根据编辑传递的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> </h6>
<aside><input class="btn-2" type="submit" value="提交"></aside>
</li>
</ul>
</form>
</fieldset>
</section>
</main>
</body>
</html>