返回这里未改变的是......登陆

这里未改变的是数据库中默认的数据,比如修改时间?,还有发布用户名?

Jokey,2019-02-19 16:12:46223

//问题描述------------------

1、在DoEdit中修改-save保存的数据,是在edit中从数据库中获取的数据

2、在编辑界面和DoEdit没有time()的更改,包括username字段的发布者。也没有更改,还是保持第一次添加的内容吗?


<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019-02-18
* Time: 22:04
*/

namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\ProductModel;
use think\facade\Request;
use think\facade\Session;

class Product extends Common
{
   public function index()
   {
       //实例化模型,查询所有信息
       $product = new ProductModel();
       //根据数据查询,按照id排序每页8条数据
       $products = $product->order('id','desc')->paginate(8);

       //模板赋值
       $this->view->products = $products;

       //渲染产品列表 //也可用return view();助手函数
      return $this->fetch();
   }

   public function add()
   {
       //渲染添加列表
       return $this->fetch();
   }
   public function DoAdd()
   {
       //获得添加过来的数据
       $data = Request::param();
       $title = $data['title'];

       //查询是否标题重复
       $info = ProductModel::where('title',$title)->find();
       if($info == true){
           return ['res'=>0,'msg'=>'产品标题重复!'];
       }
       //填充当前时间
       $data['time'] = time();
       $data['username'] = Session::get('username');

       //实例化模型
       $product = new ProductModel();

       //存入数据库
       if($product->save($data)){
           return ['res'=>1,'msg'=>'发布成功!'];
       }else{
           return ['res'=>0,'msg'=>'发布失败!'];
       }





   }

   //上传信息
   public function upload()
   {
       // 获取上传的图片信息
       $file = Request::file('img');
       // 验证图片信息并移动到指定目录
       if ($info = $file->validate(['ext' => 'jpg,jpeg,png,gif'])->move('upload')) {
           // 返回上传成功信息
           return json(['errno' => 0, 'data' => ['/upload/' . $info->getSaveName()]]);
       } else {
           // 返回错误信息
           return $file->getError();
       }
   }

   public function edit()
   {
       //接受id
       $proId = Request::param('id');

       //查询数据
       $product = ProductModel::get($proId);

       //模板赋值
       $this->view->product=$product;

       //渲染修改界面
       return $this->fetch();
   }

   public function DoEdit(){
       //获取提交过来的数据
       $data = Request::param();

       $product = new ProductModel();
       $data['time'] = time();
       $data['username'] = Session::get('username');
       
       //这里未改变的是数据库中默认的数据,比如修改时间?,还有发布用户名?
       //是在edit方法中获取数据库中的信息
       $info = $product->save([
           'title'=>$data['title'],
           'desc'=>$data['desc'],
           'content'=>$data['content'],
           'once'=>$data['once'],
           'over_night'=>$data['over_night'],
           'time'=>$data['time'],
           'username'=>$data['username'],

       ],['id'=>$data['id']]);

       if($info){
           return ['res'=>1,'msg'=>'更新成功!'];
       }else{
           return ['res'=>0,'msg'=>'更新失败!'];
       }
   }

   public function del()
   {
       //获取产品id
       $proId = Request::param('id');

       $product = new ProductModel();

       if($product->destroy($proId)){
           return ['res'=>1,'msg'=>'删除成功!'];
       }

   }
}

最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送