练习curd操作

原创2019-01-19 23:14:19149
摘要:<?php namespace app\index\controller; use think\Db; class Index {     public function index()     {      &n
<?php
namespace app\index\controller;
use think\Db;
class Index
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改(2006-2018) - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    //新增操作
    public function insert()
    {
    	$data=[
    		'goods_name'=>'华为荣耀5S手机',
    		'shop_price'=>1999,
    		'goods_number'=>2000
    	];
    	$num = Db::table('goods')->data($data)->insert();
    	$id = Db::getLastInsId();
    	return $num?'成功添加了'.$num.'条记录,id是'.$id:'添加失败';
    }

    public function insertAll()
    {
    	$data = [
    		['goods_name'=>'iPhone5S','shop_price'=>3000,'goods_number'=>1000],
    		['goods_name'=>'iPhone6S','shop_price'=>3500,'goods_number'=>1200],
    		['goods_name'=>'iPhone7S','shop_price'=>3800,'goods_number'=>3000],
    	];
    	$num = Db::table('goods')->data($data)->insertAll();
    	return $num?'成功添加了'.$num.'条记录':'添加失败';
    }
    //更新操作
    public function update()
    {
    	$num = Db::table('goods')->where('id',4)->data(['shop_price'=>Db::raw('shop_price+2300')])->update();
    	return $num?'成功更新':'更新失败';
    }
    //查询单条记录
    public function find()
    {
    	$row = Db::table('goods')->where('id','4')->find();
    	dump($row);
    }
    //查询多条记录
    public function select()
    {
    	$rows = Db::table('goods')->select();
    	dump($rows);
    }
    //删除记录
    public function delete()
    {
    	$num = Db::table('goods')->delete(4);
    	return $num?'成功删除了'.$num.'条记录':'删除失败';
    }
}


批改老师:韦小宝批改时间:2019-01-20 09:20:46
老师总结:写的很不错 看来你db类库用的还是很不错的啊 继续加油吧

发布手记

热门词条