CRUD in Yii framework

View layer:

Form:

<?php
            //Auxiliary function
use yii\helpers\Url;
?>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
   <center>
     <table>
     <form action="<?php echo Url::toRoute(['ha/add'])?>" method="post">
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Introduction</td>
<td><textarea name="text" id="" cols="30" rows="10"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td></td>
</tr>
</table>
   </form>
</center>
</body>
</html>

exhibit:

<?php 
use yii \helpers\Url;
 ?>
 <!DOCTYPE html>
 <html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>Document</title>
 </head>
 <body>
     <center>
      <table>
      <tr>
        <td>id</td>
        <td>name</td>
        <td>content</td>
        <td>操作</td>
      </tr>
      <?php foreach ($data as $key => $val) { ?>
      <tr>
       <td><?php echo $val['id']?></td>
       <td><?php echo $val['name']?></td>
       <td><?php echo $val['content']?></td>
       <td><a href="<?php echo Url::toRoute(['ha/del','id'=>$val['id']])?>">删除</a>||
                <a href="<?php echo Url::toRoute(['ha/find','id'=>$val['id']])?>">修改</a></td>
      </tr>
      <?php } ?>
   </table>
  </center>
 </body>
 </html>

Revise:

<?php
            //Auxiliary function
use yii\helpers\Url;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
   <center>
     <table>
     <form action="<?php echo Url::toRoute(['ha/upload'])?>" method="post">
       <input type="hidden" name='id' value="<?php echo $res['id']?>">
<tr>
<td>姓名</td>
<td><input type="text" name="name" value="<?php echo $res['name']?>"></td>
</tr>
<tr>
<td>Introduction</td>
<td><textarea name="content" cols="30" rows="10" value="<?php echo $res['content']?>"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td></td>
</tr>
</table>
   </form>
</center>
</body>
</html>

Controller layer:

adding data:

    public function  actionAdd(){
     $name= Yii::$app->request->post("name");
     $content =Yii::$app->request->post("content");
     $res=Yii::$app->db->createCommand("insert into yi(name,content) value('$name','$content')")->execute();
     return $this->redirect(['ha/show']);
    } 

Display data:

    public function actionShow(){
     header("content-type:text/html;charset=utf-8");
        $data= Yii::$app->db->createCommand("select * from yi")->queryAll();
        return $this->render('show',['data'=>$data]);
    }

delete data:

    public function actionDel(){
        $id= Yii::$app->request->get('id');
        $del= Yii::$app->db->createCommand("delete from yi where id = '$id'")->execute();
        return $this->redirect(['ha/show']);
    }

change the data:

    public function actionFind(){
        header("content-type:text/html;charset=utf-8");
        $id= Yii::$app->request->get('id');
        $data = yii::$app->db->createCommand("select * from yi where id ='$id'")->queryAll();
        $res=$data[0];
        return $this->render('upl',['res'=>$res]);
    }


    public function actionUpload(){
        $id = Yii::$app->request->post('id');
        $name = Yii::$app->request->post('name');
        $content = Yii::$app->request->post('content');
        Yii::$app->db->createCommand("update yi set name='$name',content='$content' where id = '$id'")->execute();
        return $this->redirect(['ha/show']);

Demonstration effect:


    }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325847830&siteId=291194637