yii框架增删改查

视图层:

表单:

<?php
            //辅助函数
使用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>简介</ 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>

展示:

<?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>

修改:

<?php
            //辅助函数
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>介绍</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>

控制器层:

添加数据:

    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']);
    } 

展示数据:

    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]);
    }

删除数据:

    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']);
    }

修改数据:

    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();
        返回$ this-> redirect(['ha / show']);

演示效果:


    }


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值