数据表:
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
`sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别',
`age` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '年龄',
`tel` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '电话',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 14 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
SET FOREIGN_KEY_CHECKS = 1;
/controller/Index.php:
<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
class Index extends \think\Controller
{
public function index()
{
//分页
$result = Db::table('user')->paginate(10);
$page = $result->render();
$this->assign('data_list', $result);
$this->assign('page', $page);
return $this->fetch('user');
}
//新增编辑
public function add_user()
{
//编辑
$id=input('id');
if($id>0){
if(request()->isPost()){
$data['name'] = input('name');
$data['sex'] = input('sex');
$data['age'] = input('age');
$data['tel'] = input('tel');
$res = Db::table('user')->where('id',input('post.id'))->update($data);
// echo db('user')->getlastsql();die;
if($res){
$this->success('更新成功', 'Index/index');
}else{
$this->error('更新失败!');
}
}else{
$id = input('id');
$info = db('user')->where('id',$id)->find();
$this->assign('info',$info);
return $this->fetch('add_user');
}
}else{
//添加
if(request()->isPost()){
$data = input('post.');
$add_res = db('user')->insert($data);
if($add_res){
$this->success('新增成功', 'Index/index');
}else{
return $this->error('添加失败!');
}
}
return $this->fetch('add_user');
}
}
//删除
public function del(){
if(request()->isGet()){
$del_id = input('id');
$res = Db::table('user')->where('id',$del_id)->delete();
if($res){
$this->success('删除成功!','index');
}else{
$this->error('删除失败!');
}
}
}
}
?>
/view/index/add_user.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="{:url('index/add_user')}" method="post">
姓名 :<input type="text" name="name" {notempty name="$info.name"} value="{$info.name}"{/notempty}><br><br>
性别 :
男 <input type='radio' name='sex' {if condition="isset($info) AND $info['sex'] eq '男'"}checked="checked" {/if} value='男'/>
女 <input type='radio' name='sex'{if condition="isset($info) AND $info['sex'] eq '女'"}checked="checked" {/if} value='女'/>
年龄 :<input type="text" name="age" {notempty name="$info.age"} value="{$info.age}"{/notempty}><br><br>
电话 :<input type="text" name="tel" {notempty name="$info.tel"} value="{$info.tel}"{/notempty}><br><br>
<input type="hidden" {notempty name="$info.id"}value="{$info['id']}" {/notempty}name="id">
<input type="submit" value="确定">
<input type="button" value="取消">
</form>
</body>
</html>
/view/index/user.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h4>用户信息管理</h4>
<a href="{:url('index/Index/add_user')}">添加</a>
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<td>ID</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>电话</td>
<td>操作</td>
</tr>
{volist name="data_list" id="vo"}
<tr>
<td>{$vo.id}</td>
<td>{$vo.name}</td>
<td>{if condition="$vo.sex eq '男'"}男{elseif condition="$vo.sex eq '女'"}女{else/}保密{/if}</td>
<td>{$vo.id}</td>
<td>{$vo.tel}</td>
<td><a href="{:url('index/add_user',['id' => $vo['id']])}">编辑</a> | <a href="{:url('index/del',['id' => $vo['id']])}">删除</a></td>
</tr>
{/volist}
</table>
<div>{$page}</div>
</body>
</html>
难点详解:
1.合并编辑添加:
添加不需要获取id去引用数据而编辑需要 用户去点击编辑->通过获取id获取一条数据->通过变量输出到页面。可以通过判断id>0,判断为编辑否则为添加。
2.合并添加和编辑后,二者html页面在同一页面,男女radio选终于被选中的状态不同。
所以要在input的框中进行判断:首先要判断性别是否已被设置值 isset()通过AND逻辑符,同一条件下还要满足数据库中传来选中的值。