abstrakt://問題描述------------------1、在DoEdit中修改-save保存的數(shù)據(jù),是在edit中從數(shù)據(jù)庫(kù)中獲取的數(shù)據(jù)2、在編輯界面和DoEdit沒有time()的更改,包括username字段的發(fā)布者。也沒有更改,還是保持第一次添加的內(nèi)容嗎?<?php/** * Created by PhpStorm. * User: Administrator * Date: 2019-02-
//問題描述------------------
1、在DoEdit中修改-save保存的數(shù)據(jù),是在edit中從數(shù)據(jù)庫(kù)中獲取的數(shù)據(jù)
2、在編輯界面和DoEdit沒有time()的更改,包括username字段的發(fā)布者。也沒有更改,還是保持第一次添加的內(nèi)容嗎?
<?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()
{
//實(shí)例化模型,查詢所有信息
$product = new ProductModel();
//根據(jù)數(shù)據(jù)查詢,按照id排序每頁8條數(shù)據(jù)
$products = $product->order('id','desc')->paginate(8);
//模板賦值
$this->view->products = $products;
//渲染產(chǎn)品列表 //也可用return view();助手函數(shù)
return $this->fetch();
}
public function add()
{
//渲染添加列表
return $this->fetch();
}
public function DoAdd()
{
//獲得添加過來的數(shù)據(jù)
$data = Request::param();
$title = $data['title'];
//查詢是否標(biāo)題重復(fù)
$info = ProductModel::where('title',$title)->find();
if($info == true){
return ['res'=>0,'msg'=>'產(chǎn)品標(biāo)題重復(fù)!'];
}
//填充當(dāng)前時(shí)間
$data['time'] = time();
$data['username'] = Session::get('username');
//實(shí)例化模型
$product = new ProductModel();
//存入數(shù)據(jù)庫(kù)
if($product->save($data)){
return ['res'=>1,'msg'=>'發(fā)布成功!'];
}else{
return ['res'=>0,'msg'=>'發(fā)布失敗!'];
}
}
//上傳信息
public function upload()
{
// 獲取上傳的圖片信息
$file = Request::file('img');
// 驗(yàn)證圖片信息并移動(dòng)到指定目錄
if ($info = $file->validate(['ext' => 'jpg,jpeg,png,gif'])->move('upload')) {
// 返回上傳成功信息
return json(['errno' => 0, 'data' => ['/upload/' . $info->getSaveName()]]);
} else {
// 返回錯(cuò)誤信息
return $file->getError();
}
}
public function edit()
{
//接受id
$proId = Request::param('id');
//查詢數(shù)據(jù)
$product = ProductModel::get($proId);
//模板賦值
$this->view->product=$product;
//渲染修改界面
return $this->fetch();
}
public function DoEdit(){
//獲取提交過來的數(shù)據(jù)
$data = Request::param();
$product = new ProductModel();
$data['time'] = time();
$data['username'] = Session::get('username');
//這里未改變的是數(shù)據(jù)庫(kù)中默認(rèn)的數(shù)據(jù),比如修改時(shí)間?,還有發(fā)布用戶名?
//是在edit方法中獲取數(shù)據(jù)庫(kù)中的信息
$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()
{
//獲取產(chǎn)品id
$proId = Request::param('id');
$product = new ProductModel();
if($product->destroy($proId)){
return ['res'=>1,'msg'=>'刪除成功!'];
}
}
}