thinkphp5操作mysql数据库入库信息方法整理

添加用户add.html

<!--header-->
{include file="public/_meta" /}
<article class="page-container">
  <form class="form form-horizontal" id="form-admin-add" method="post" action="{:url('admin/add')}">
    <div class="row cl">
      <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>管理员名:</label>
      <div class="formControls col-xs-8 col-sm-9">
        <input type="text" class="input-text" value="" placeholder="" id="username" name="username">
      </div>
    </div>
    <div class="row cl">
      <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>密码:</label>
      <div class="formControls col-xs-8 col-sm-9">
        <input type="password" class="input-text" autocomplete="off" value="" placeholder="密码" id="password" name="password">
      </div>
    </div>

    <div class="row cl">
      <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
        <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
      </div>
    </div>
  </form>
</article>
{include file="public/_footer" /}

</body>
</html>

创建公共lib库

thinkphpwu/application/common/lib 中创建AdminAuth.php 文件,用于验证密码信息,封装之后,可以复用

<?php

namespace app\common\lib;

class AdminAuth
{
    //得到密码加密后的信息
    public static function setPassword($password)
    {
       return md5($password.config('app.admin_password_pre'));
    }
}

在这里插入图片描述

封装配置文件参数

在thinkphpwu/application/extra 目录中创建app.php

<?php

return [
    'admin_password_pre' => '_qipa250',//后台管理员密码加密后缀
];

在这里插入图片描述
调用方法:

config(‘文件名称.参数’);
config(‘app.admin_password_pre’);

创建model层

<?php

namespace app\common\model;


use think\Model;


class AdminUser extends Model
{
    //新增管理员信息入库
    public function add($params)
    {
        //判断是否为数组
        if (!is_array($params)) {
            //抛出异常
            exception('传递的数据不合法!');
        }
        //常用变量
        $params['status'] = 0;
        $params['create_time'] = date('Y-m-d H:i:s', time());
        //入库
        $this->allowField(true)->save($params);
        //返回主键id
        return $this->getLastInsID();
    }
}

admin控制器add方法

<?php

namespace app\admin\controller;

use app\common\lib\AdminAuth;
use think\Controller;
use think\Exception;


class Admin extends Controller
{
    public function add()
    {

        try {
            //判断是否为post提交
            if (request()->isPost()) {
                //得到post提交的数据
                $data = (input('post.'));
                //实例化校验规则类库
                $validate = validate('AdminUser');
                //校验
                if (!$validate->check($data)) {
                    $this->error($validate->getError());
                }
                
                //组装数据入库
                $params = array(
                    'username' => $data['username'],
                    'password' => AdminAuth::setPassword($data['password']),
                );
                //判断是否已经存在
                $admin_user_info = model('AdminUser')->get(['username' => $data['username']]);
                if ($admin_user_info) {
                    $this->error('该用户名称已经存在,请勿重复添加!');
                }
                //入库
                $admin_user_id = model('AdminUser')->add($params);
                if (!$admin_user_id) {
                    $this->error('管理员添加失败!');
                }          
              $this->success('添加管理员成功!', 'index/index');
            } else {
                //加载视图文件
                return $this->fetch();
            }
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }
}

在这里插入图片描述
在这里插入图片描述数据库中查看

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值