php后台添加删除产品代码,产品的添加,编辑,删除操作

摘要:<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019-01-31 * Time: 10:18 */namespace app\admin\controller;use app\admin\controller\Common;use think\facade\Request;use think\facade\Ses

/**

* Created by PhpStorm.

* User: Administrator

* Date: 2019-01-31

* Time: 10:18

*/

namespace app\admin\controller;

use app\admin\controller\Common;

use think\facade\Request;

use think\facade\Session;

use app\admin\model\ProductModel;

class Product extends Common

{

public  function index()

{

$product = new ProductModel();

$products = $product->order('id','desc')->paginate(5);

$this->view->products =$products;

return $this->fetch();

}

public function add()

{

return $this->view->fetch();

}

public function edit()

{

$id =Request::param('id');

$product =ProductModel::get($id);

$this->view->product = $product;

return $this->fetch();

}

public function doadd()

{

$data =Request::param();

$title =$data['title'];

$info = ProductModel::where('title',$title)->find();

if ($info)

{

return ['res'=>0,'msg'=>'产品名称重复'];

}

$data['time']=time();

$data['username']=Session::get('username');

$produst = new ProductModel();

$res =$produst->save($data);

if ($res)

{

return ['res'=>1,'msg'=>'保存成功'];

}

else

{

return  ['res'=>0,'msg'=>'保存失败'];

}

}

public function upload()

{

$file= Request::file('img');

$info = $file->validate(['ext'=>'jpg,jpeg,png,gif'])-> move('upload');

if ($info)

{

return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]);

}

else

{

return $file->getError();

}

} //上传图片

public function doedit() //编辑商品

{

$data = Request::param();

$product = new ProductModel();

$data['time']= time();

$data['username']= Session::get('username');

$info = $product-> save([

'title'=>$data['title'],

'desc'=> $data['desc'],

'content'=> $data['content'],

'price1'=>$data['price1'],

'price2'=> $data['price2'],

'time'=> $data['time'],

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

if($info)

{

return ['res'=>1, 'msg'=>'更新成功'];

}

else

{

return ['res'=>0,'msg'=>'更新失败'];

}

}

public function del()

{

$id= Request::param('id');

$product = new ProductModel();

$res = $product->destroy($id);

if ($res)

{

return ['res'=>1,'msg'=>' 删除成功'];

}

}

}

批改老师:天蓬老师批改时间:2019-02-03 15:06:10

老师总结:下次不要这样提交了, 一行注释不写, 让人猜吗

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值