关于Yii框架基础的增删查改

来源:互联网 发布:python 遥感贝叶斯 编辑:程序博客网 时间:2024/05/22 06:18
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
class DemoController extends Controller{
    public $enableCsrfValidation = false;
    public function actionIndex(){
        return $this->render("index");
          }
          public function actionAdd()
        {
            $name = Yii::$app->request->post('name');
            $res=Yii::$app->db->createCommand("insert into diyi(`name`) value('$name')")->execute();
            return $this->redirect(['demo/show']);
        }
        public function actionShow()
        {
            header("content-type:text/html;charset=utf-8");
            $data = Yii::$app->db->createCommand("select * from `diyi`")->queryAll();
            return $this ->render('show',['data'=>$data]);
        }
        public function actionDel()
        {
            $id = Yii::$app->request->get('id');
            $del = Yii::$app->db->createCommand("delete from diyi where id = '$id'")->execute();
            return $this->redirect(['demo/show']);
        }
        public function actionFind()
        {
            $id = Yii::$app->request->get('id');
            $data = Yii::$app->db->createCommand("select * from diyi where id = '$id'")->queryAll();
            $res=$data[0];
            return $this->render('find',['res'=>$res]);
        }
}