控制器的增删改查操作

原创 2019-01-02 17:42:12 311
摘要:<?php namespace app\index\controller; use think\Db; class Query { public function find() { // $result = Db::name('tp_goods') // ->find
<?php
namespace app\index\controller;
use think\Db;

class Query
{
	public function find()
	{
		// $result = Db::name('tp_goods')
		// ->find(5);

		// $res = Db::table('tp_goods')
		// 	->where('price','>',10)
		// 	->limit(2)
		// 	->select();

		$res = Db::table('tp_goods')
			->field(['pname'=>'name','price'])
			->where('price',5)
			->find();
		dump($res);
	}

	public function select()
	{
		$res = Db::table('tp_goods')
			->field(['pname','price'])
			->where('price','>',10)
			->select();
		dump($res);
	}

	public function insert()
	{
		$data = [
			'typeid' => 15,
			'pname'  =>	'七妹槟榔',
			'price'	 =>	10.00,
			'pcount' =>	40	
		];
		// $res = Db::name('tp_goods')
		// 	->data($data)
		// 	->insert($data);
		// $id = Db::getLastInsId();
		$res = Db::table('tp_goods')
			->insertGetId($data);
		return '插入数据成功,id为'.$res;

	}
	public function update()
	{
		Db::name('tp_goods')
			->where('pid',14)
			->data(['pname'=>'八妹槟榔'])
			->update();
	}
	public function delete()
	{
		$res = Db::name('tp_goods')
			->where('pid',15)
			->delete();
		return $res ? '删除成功' : '删除失败';
	}
}

感觉使用TP的函数来操作更加麻烦,但是TP多了更多的安全机制

批改老师:韦小宝批改时间:2019-01-03 09:08:05
老师总结:那可能是因为你对tp中的方法还是不太熟悉,等多写几个项目的时候,你就感觉tp中内置的方法还是很方便的

发布手记

热门词条