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,
               )); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题