//表单页面
<center> <form action="?r=test/adds" method="post"> <table> <tr> <td>姓名</td> <td><input type="text" name="name"/></td> </tr> <tr> <td>班级</td> <td><input type="text" name="class"/></td> </tr> <tr> <td>年龄</td> <td><input type="text" name="age"/></td> </tr> <tr> <td><input type="submit" value="提交"/></td> <td></td> </tr> </table> </form> </center>//控制器
namespace frontend\controllers; use app\models\Test; use yii\web\Controller; class TestController extends Controller{ public $enableCsrfValidation=false; public function actionAdd(){ return $this->render('add'); } //接值 public function actionAdds(){ $request=\Yii::$app->request; $name=$request->post('name'); $class=$request->post('class'); $age=$request->post('age'); //添加 $test=new Test(); $test->name="$name"; $test->class="$class"; $test->age="$age"; $test->save(); $this->redirect('?r=test/update'); } //修改 public function actionUpdate(){ $test=Test::find()->where(['id'=>1])->one(); $test->age='26'; echo $test->save(); } //删除 public function actionDelete(){ $test=Test::find()->where(['id'=>5])->one(); $test->delete(); } //查询 public function actionSelect(){ $test=Test::find()->asArray()->all(); print_r($test); } }