data is saved twice to the database

2019-07-02 00:30发布

Here is my code:

public function actionPostTest()
{

    if(isset($_POST['Test']))
    {
        $model = new Test();
        $model->attributes = $_POST['Test'];
        if($model->save())
        {
            $this->redirect('postTest');
        }
    }

    $this->render('posttest', array('model'=>new Test()));
}

This is saving the data which coming from a form twice to the database.

What is wrong?

标签: php yii
2条回答
Rolldiameter
2楼-- · 2019-07-02 00:33

Try disabling ajax validation.

Set enableAjaxValidation to false in that view.

Actually there POST happens two times. First Ajax validation and second form submit. You can confirm it by using httpfox in firefox.

查看更多
别忘想泡老子
3楼-- · 2019-07-02 00:46

You can try by this

$model = new Test();
if(isset($_POST['Test']))
{       
    $model->attributes = $_POST['Test'];
    if($model->save())
    {
        unset($_POST['Test']);
        $this->redirect('postTest');
    }
}
查看更多
登录 后发表回答