yii2.0 的数据的 增

本文展示了一个使用Yii框架进行数据增加操作的具体实例。通过controller层与model层的交互实现数据提交与验证,并最终保存数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

增加数据

    /**
     * 添加数据  controller 层
     */

 

  //引入对应的model类

  use app\models\About;

  //定义对应的方法固定的actionxxxx -- 增加数据

  public function actionAdd(){

        //判断是否是提交过来的数据

        $request = Yii::$app->request;

        if ($request->isPost){

                //实力话model

            $model_about = new About();

            $model_ok = $model_about->adddata();

            if ($model_ok) {

                //添加成功 跳转到 index 方法
                
                return $this->redirect(Url::toRoute('/about/index'));

                exit;

            }
        }

        //添加数据

        return $this->renderPartial('add');
    }

 

    /**
     * 添加数据  models层
     */

    public function adddata(){

            //接收数据

        $this->about_name = trim($_POST['about_name']);
        $this->about_link = trim($_POST['about_link']);
        $this->about_num = trim($_POST['about_num']);
        $this->about_show = trim($_POST['about_show']);
        $this->about_status = trim($_POST['about_status']);        

        return $this->save();
    }

转载于:https://www.cnblogs.com/laowenBlog/p/5280418.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值