views页面
index.php
<?php // header("content-type:text/html;charset=utf-8"); /* @var $this yii\web\View */ $this->title = "yii增删改查"; ?> <center> <form action="?r=curd/add" method="post"> <!-- <input type="hidden" value="TSol1G2RuGP9heKFYtHazIxVwBHD9UA-k2tGhck-sN_15tbNHcHRsbAzKnUk0g_zqAd6Bq9hFhyZ-u6pVwvS4w==" name="_csrf">--> <table> <tr> <td>用户名</td> <td><input type="text" name="username"></td> </tr> <tr> <td><input type="submit" value="添加"></td> </tr> </table> </form> </center>
show.php
<?php use yii\helpers\Html; use yii\widgets\LinkPager; ?> <center> <table border="1"> <th>id</th> <th>用户名</th> <th>操作</th> <?php foreach ($data as $v): ?> <tr> <td> <?= Html::encode("{$v->id}") ?> </td> <td> <?= Html::encode("{$v->username}") ?> </td> <td> <a href="?r=curd/del&id=<?= Html::encode("{$v->id}") ?>">删除</a>||<a href="?r=curd/find&id=<?= Html::encode("{$v->id}") ?>">修改</a> </td> </tr> <?php endforeach; ?> </table> <?= LinkPager::widget(['pagination' => $pagination]) ?> </center>update.php
<?php /* @var $this yii\web\View */ use yii\helpers\Html; $this->title = "yii修改"; ?> <center> <form action="?r=curd/update" method="post"> <table> <input type="hidden" name="id" value="<?= Html::encode("{$data->id}") ?>"> <tr> <td>用户名</td> <td><input type="text" name="username" value="<?= Html::encode("{$data->username}") ?>"></td> </tr> <tr> <td><input type="submit" value="修改"></td> </tr> </table> </form> </center>
models
<?php namespace app\models; use yii\db\ActiveRecord; use yii\base\Model; class User_1 extends ActiveRecord { }controllers
<?php namespace app\controllers; use app\models\User_1; use Yii; use yii\data\Pagination; // use yii\filters\AccessControl; use yii\web\Controller; use yii\web\Response; // use yii\filters\VerbFilter; // use app\models\LoginForm; // use app\models\ContactForm; class CurdController extends Controller { public function actionIndex() { return $this->render('index'); } //添加 public function actionAdd() { // $admin = new User_1(); // $username=$_POST['username']; // $admin->username = $username; // if($admin->save() > 0){ echo "添加成功"; }else{ echo "添加失败"; } $model=new User_1(); $request = Yii::$app->request; if ($request->isPost) { $post=$request->post(); $model->username=$post['username']; $model->save(); } echo '<script>alert("添加成功");location.href="?r=curd/select"</script>'; } //查询 public function actionSelect() { $query = User_1::find(); $pagination = new Pagination([ 'defaultPageSize' => 5, 'totalCount' => $query->count(), ]); $countries = $query ->offset($pagination->offset) ->limit($pagination->limit) ->all(); return $this->render('show', [ 'data' => $countries, 'pagination' => $pagination, ]); // $model=new User_1(); // $data=$model->find()->all(); // return $this->render('show',['data'=>$data]); } //删除 public function actionDel(){ $request = Yii::$app->request; $id=$request->get("id"); $data=User_1::findOne($id)->delete(); if($data){ echo '<script>alert("删除成功");location.href="?r=curd/select"</script>'; } else{ echo '<script>alert("删除失败");location.href="?r=curd/select"</script>'; } } //查询一条 public function actionFind() { $request = Yii::$app->request; $id=$request->get("id"); $model=new User_1(); $data=$model->find()->where("id=$id")->one(); return $this->render('update',['data'=>$data]); } public function actionUpdate(){ $request = Yii::$app->request; $id=$request->post("id"); $model=new User_1(); $model=$model->findOne($id); $post=$request->post(); $model->username=$post['username']; $res=$model->save(); if($res){ echo '<script>alert("修改成功");location.href="?r=curd/select"</script>'; } else{ echo '<script>alert("修改失败");location.href="?r=curd/find"</script>'; } } }