ThinkPHP5.0的基本CURD操作

来源:互联网 发布:阿里云课堂app 编辑:程序博客网 时间:2024/05/29 04:29

第一步:在浏览器中运行框架,完毕后。在application/index中新建model目录和view目录(view目录里并建index目录)。然后在application里的database.PHP里配置数据库名 用户名 密码。

第二步:在model目录里新建User.php模型层.

新建User.php模型层.

User.php中内容为:

<?phpnamespace app\index\model;use think\Db;//引入Db     use think\Model;//引入Modelclass User extends Model{        protected $table='user';//表名        //增加        function insertData($data){                return Db::table($this->table)->insertGetId($data);        }        //查询        function show(){                return Db::table($this->table)->select();            }        //删除        function deleteData($id){                return Db::table($this->table)->where('uid','=',$id)->delete();            }        //查询单条        function findData($id){                return Db::table($this->table)->where('uid','=',$id)->find();            }        //修改        function updateData($data,$id){                return Db::table($this->table)->where('uid','=',$id)->update($data);            }}?>

第三步:controller目录里控制器index.php里面内容为:

* 控制器内容*

<?phpnamespace app\index\controller;use think\controller;//引入控制器use think\Request;//引入requestuse app\index\model\User;//引入模型层header("content-type:text/html;charset=UTF-8");//防乱码class Index extends Controller{    //表单页面        public function index(){                return view('index');                //return $this->fetch('show');        }        //添加        public function insert(){                $Request=Request::instance();//调用Request中instance方法                $data=$Request->post();//接值                //print_r($data);                $user=new User;//实例化model                $result=$user->insertData($data);//执行添加语句                if($result){                       return Redirect('Index/show');                    //echo "<script>alert('新增成功');localtion.href='{:url('Index/show')}'</script>";                    //$this->success('新增成功','Index/show');                }else{                    $this->error('新增失败');                }        }        //展示        public function show(){                $user=new User;//实例化model                $arr=$user->show();//执行查询                return view('show',['arr'=>$arr]);        }        //删除        public function delete() {                $Request=Request::instance();                $id=$Request->get('id');                $user=new User;                $result=$user->deleteData($id);                if($result){                        return Redirect('Index/show');                }else{                        $this->error('删除失败');                }        }        //修改页面        public function update(){                $Request=Request::instance();                $id=$Request->get('id');                $user=new User;                $res=$user->findData($id);//查询单条                //print_r($res);die;                return view('update',['res'=>$res]);        }        //执行修改        public function save(){                $Request=Request::instance();                $id=$Request->post('uid');                $Request=Request::instance();                $data=$Request->post();//接修改之后的数据                $user=new User;                $result=$user->updateData($data,$id);//修该语句                if($result){                        return Redirect('Index/show');                }else{                        $this->error('修改失败');                }        }}?>

第四步:在视图层index目录里新建index.html show.html update.html里面内容分别为:

视图层页面

index.html中:





用户注册


用户名 密码



show.html中:





用户列表










{volist name=”arr” id=”vo”}






{/volist}
ID 用户名 密码 操作 {$vo.uid} {$vo.username} {$vo.password} 修改


update.html中:





修改信息


 <form action="{:url('Index/save')}" method="post"> <input type="hidden" name="uid" value="{$res['uid']}"/>    <table>       <!--  <tr>            <td></td>            <td><input type="text" name="uid" value="{$res['uid']}"/></td>        </tr>  -->           <tr>             <td>用户名</td>             <td><input type="text" name="username" value="{$res['username']}"/></td>         </tr>         <tr>             <td>密码</td>             <td><input type="password" name="password" value="{$res['password']}" /></td>         </tr>         <tr>             <td><input type="submit" value="修改" /></td>             <td></td>         </tr>     </table> </form>



原创粉丝点击