dsbfbz75185 2016-07-06 05:59
浏览 70
已采纳

如何在yii 1框架中将表单发布数据插入数据库

this is my form in view

<div class="form">
<?php echo CHtml::errorSummary($model); ?>

<div class="row">
    <?php echo CHtml::activeLabel($model,'Your Name'); ?>
    <?php echo CHtml::activeTextField($model,'regname') ?>
</div>

<div class="row">
    <?php echo CHtml::activeLabel($model,'Your Password'); ?>
    <?php echo CHtml::activePasswordField($model,'regpass') ?>
</div>

<div class="row">
    <?php echo CHtml::activeLabel($model,'Email Address'); ?>
    <?php echo CHtml::activePasswordField($model,'regemail') ?>
</div>
 <div class="row">
    <?php echo CHtml::activeLabel($model,'Contact'); ?>
    <?php echo CHtml::activePasswordField($model,'regcontact') ?>
</div>

<div class="row submit">
    <?php echo CHtml::submitButton('Login'); ?>
</div>

please help me to save this data from my controller to model this is my model //-- set Table

public function tableName(){
    return 'user';
    }

    public function attributeLabels()
        {
          return array(
        'username'=>'Your username for the game',
        'password'=>'Your password for the game',
         'email'=>'Needed in the event of password resets',
                );
        }

and this is my controller

public function actionRegister2()
    {
        $model=new RegisterForm;
        if(isset($_POST['RegisterForm']))
        {
            echo 'done';
        }
        $this->render('register2',array(
                'model'=>$model,
            )); 

i an new in yii framework so didn't get a good tutorial for it, please suggest me how i insert the data into database by submitting a form

  • 写回答

1条回答 默认 最新

  • duanchi3109 2016-07-06 06:11
    关注

    you can add this code in your controller:

    public function actionRegister2()
    {
         $model=new RegisterForm;
         if(isset($_POST['RegisterForm']))
         {
             $model->attributes = $_POST['RegisterForm'];
             if ($model->save()) {
                Yii::app()->user->setFlash('success', 'You have successfully added.');
                $this->redirect(array('index'));
            }
             // or if(!$model->save()){ print_r($model->getErrors())} 
        }
        $this->render('register2',array(
                        'model'=>$model,
               )); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装