thinkphp3增、删、改、查
写一个商品类
<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller{
//商品列表页面
function goods_list(){
$good = M('goods');
$goodlist = $good->select(); //将sw_goods表中的数据全都查出来
$this->assign('list',$goodlist); //将数据注册到模版中
$this->display();
}
//商品添加页面
function goods_add(){
if(IS_POST){ //判断是否为post传值
$data['goods_name'] = $_POST['goods_name'];
$data['goods_price'] = $_POST['shop_price'];
$msg = '添加失败';
if(M('goods')->add($data)){
$msg = '添加成功';
}
$this->redirect('goods_list',array(),3,$msg);
}
//商品分类注册进视图
$cat = M('category')->select();
$this->assign('catlist',$cat); //将数据注册到模版中
$this->display();
}
//商品编辑页面
function goods_edit($id){
if(IS_POST){
$goods = M('goods');
$data = $goods->create();
$msg = '修改失败';
if($goods->save($data)){
$msg = '修改成功';
}
$this->redirect('goods_list',array(),3,$msg);
}
$list = M('goods')->select($id);
$this->assign('good',$list[0]);
$this->display();
}
//商品删除
function del($id){
if(M('goods')->delete($id)){
$this->success('删除成功',U('goods_list'));
}else{
$this->error('删除失败',U('goods_list'));
}
}
}
编辑只能改第一条的name