ThinkPHP框架学习之CRUD

User模块UserAction.class.php

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!--?php
class UserAction extends Action {
    public function index(){
        $m=M('User');//操作的数据库表
        $arr=$m--->select();//查询数据库
        $this->assign('data',$arr);//
        $this->display();
    }
     
    /*
     * 向数据库插入数据
     **/
    public function add(){
        $m=M('User');
        $m->username=$_POST['username'];
        $m->sex=$_POST['sex'];
        $num=$m->add();
        if ($num>0){
            $this->success('添加成功','index');//添加成功并返回首页
        }else {
            $this->error('添加失败');
        }
    }
    /*
     * 显示添加表单
     **/
    public function addform(){
        $this->display();
    }
}
?>

首页模板文件index.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    <title></title>
    <script>
        function jump(){
            window.location='__URL__/addform';
        }
    </script>
 
 
    <volist name="data" id="vo">
        </volist><table border="1">
        <tbody><tr>
            <th>ID号</th>
            <th>用户名</th>
            <th>性别</th>
            <th>操作</th>
        </tr>
        <tr>
            <td>{$vo.id}</td>
            <td>{$vo.username}</td>
            <td>{$vo.sex}</td>
            <td>删除|修改</td>
        </tr>
         
    </tbody></table>
    <button onclick="jump()">添加用户(长春九龙男科医院)</button>
 
<!--html>

添加表单模板addform.html

?
1
2
3
4
5
6
7
8
9
<title></title>
 
 
<form action="__URL__/add" method="post">
    姓名:<input type="text" name="username"><br>
    性别:男<input type="radio" name="sex" value="1" checked="true">
    女<input type="radio" name="sex" value="0"><br>
    <input type="submit" value="添加用户">
</form>
原文地址:https://www.cnblogs.com/hengyi123/p/3754600.html