PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > ThinkPhp教程 > 正文

    ThinkPHP框架学习之CRUD

    作者:w634381967来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:ThinkPHP框架学习之CRUD,分享一下学习笔记User模块UserAction.class.php复制代码 代码如下:<!--?phpclass UserAction extends Action...
    ThinkPHP框架学习之CRUD,分享一下学习笔记
    User模块UserAction.class.php

     
    复制代码 代码如下:
    <!--?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
     
    复制代码 代码如下:
    <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
     
    复制代码 代码如下:
    <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> 

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-83-802-1.html
    相关热词搜索: ThinkPHP框架