abstract:<?php /** * Created by PhpStorm. * User: Admin * Date: 2019/4/5 * Time: 20:03 */ namespace app\admin\controller; use
<?php /** * Created by PhpStorm. * User: Admin * Date: 2019/4/5 * Time: 20:03 */ namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\NewsModel; use app\admin\model\NewsPicModel; use think\facade\Request; use think\facade\Session; class NewsPic extends Common { //渲染頁面 public function index() { //查詢數(shù)據(jù) $new_pic = NewsPicModel::all(); $this->view->new=$new_pic; return $this->fetch(); } //渲染添加頁面 public function add() { //查詢新聞title數(shù)據(jù) $new = NewsModel::all(); $this->view->new=$new; return $this->fetch(); } //圖片上傳 public function upload() { //獲取圖片信息 $pic = Request::file('file'); //驗證圖片并移到指定目錄下 if($info = $pic->validate(['ext'=>'jpg,png,gif'])->move('smallImg')){ //獲取圖片路勁 return json([1,'上傳成功','data'=>'/smallImg/'.$info->getSaveName()]); }else{ return $pic->getError(); } } //添加縮略圖數(shù)據(jù) public function DoAdd() { //獲取前臺提交過來的數(shù)據(jù) $data = Request::param(); $data['username']=Session::get('username'); $data['time']=time(); //判斷數(shù)據(jù)是否添加成功 $news = new NewsPicModel(); if($news->save($data)){ return ['res'=>1,'msg'=>'添加成功!']; }else{ return ['res'=>0,'msg'=>'添加失?。?#39;]; } } //數(shù)據(jù)刪除 public function del() { //獲取前臺提交的id $id = Request::param('id'); $res = NewsPicModel::destroy($id); if($res){ return ['res'=>0,'msg'=>'刪除成功!']; }else{ return ['res'=>0,'msg'=>'刪除失?。?#39;]; } } }
Correcting teacher:西門大官人Correction time:2019-04-08 10:12:35
Teacher's summary:數(shù)據(jù)的添加,盡量手動賦值,這樣可以避免前臺傳過來非法的字段,造成數(shù)據(jù)庫報錯,泄漏數(shù)據(jù)庫信息