thinkphp5写入 数据库

目录结构

Admin.php

[php]  view plain  copy
  1. <?php  
  2. namespace app\admin\controller;  
  3. use think\Controller;  
  4. class Admin extends Controller{  
  5.     public function lst(){  
  6.         return $this->fetch('lst');  
  7.   
  8.     }  
  9.     public function add(){  
  10.         //判断页面是否提交  
  11.         if(request()->isPost()){  
  12. //          dump(input('post.')); //打印接收到的参数  
  13.   
  14.             $data = [       //接受传递的参数  
  15.                 'username' => input('username'),  
  16.                 'password' => md5(input('password')),  
  17.             ];  
  18.               
  19.         /*  Db('表名') 数据库助手函数*/  
  20.             if(Db('admin') -> insert($data)){        //添加数据  
  21.                 return $this->success('添加成功','lst'); //成功后跳转  lst 界面  
  22.             }else{  
  23.                 return $this->error('添加管理员失败');  
  24.             }  
  25.             return;  
  26.         }  
  27.         return $this->fetch('add');  
  28.   
  29.     }  
  30. }  

add.html

[php]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.         <title>后台</title>  
  6.         <style type="text/css">  
  7.             h1{  
  8.                 color: #f56868;  
  9.                 text-align: center;  
  10.             }  
  11.         </style>  
  12.     </head>  
  13.     <body>  
  14.         <h1>我是add</h1>    
  15.         <!-- action="" 值如果为空 提交到调用方法 -->  
  16.         <form role='form' action="" method="post">  
  17.             管理员名:<input type="text" name="username" /><br /><br />  
  18.             管理员角色:<input type="text" name="password" />  
  19.             <input type="submit" value="提交"/>  
  20.         </form>  
  21.       
  22.     </body>  
  23. </html>  

lst.html

[php]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.         <title>后台</title>  
  6.     </head>  
  7.     <body>  
  8.         <div>我是lst</div>  
  9.         <a href="{:url('admin/add')}">跳转add</a>  
  10.     </body>  
  11. </html>  

database.php  链接数据库

[php]  view plain  copy
  1. <?php  
  2. return [  
  3.     // 数据库类型  
  4.     'type'            => 'mysql',  
  5.     // 服务器地址  
  6.     'hostname'        => '127.0.0.1',  
  7.     // 数据库名  
  8.     'database'        => 'blog',  
  9.     // 用户名  
  10.     'username'        => 'root',  
  11.     // 密码  
  12.     'password'        => 'root',  
  13.     // 端口  
  14.     'hostport'        => '',  
  15.     // 连接dsn  
  16.     'dsn'             => '',  
  17.     // 数据库连接参数  
  18.     'params'          => [],  
  19.     // 数据库编码默认采用utf8  
  20.     'charset'         => 'utf8',  
  21.     // 数据库表前缀  
  22.     'prefix'          => 'tp_',  
  23.     // 数据库调试模式  
  24.     'debug'           => true,  
  25.     // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)  
  26.     'deploy'          => 0,  
  27.     // 数据库读写是否分离 主从式有效  
  28.     'rw_separate'     => false,  
  29.     // 读写分离后 主服务器数量  
  30.     'master_num'      => 1,  
  31.     // 指定从服务器序号  
  32.     'slave_no'        => '',  
  33.     // 是否严格检查字段是否存在  
  34.     'fields_strict'   => true,  
  35.     // 数据集返回类型  
  36.     'resultset_type'  => 'array',  
  37.     // 自动写入时间戳字段  
  38.     'auto_timestamp'  => false,  
  39.     // 时间字段取出后的默认时间格式  
  40.     'datetime_format' => 'Y-m-d H:i:s',  
  41.     // 是否需要进行SQL性能分析  
  42.     'sql_explain'     => false,  
  43. ];