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

时间:2016-07-06 05:59:13

标签: php yii

这是我在视野中的表格

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

请帮我将这些数据从我的控制器保存到型号 这是我的模特 // - 设置表

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

这是我的控制器

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

我是一个新的yii框架所以没有得到一个很好的教程,请建议我如何通过提交表单将数据插入数据库

1 个答案:

答案 0 :(得分:0)

您可以在控制器中添加此代码:

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