2

I'm new to PHP and Yii framework. how to insert multiple questions in database

This is Form view code

<?php echo $form->textField($model,'questions',array('id'=>"content_#index#_question textBox")); ?>

<?php echo $form->textField($model,'questions',array('id'=>"content_#index#_question textBox")); ?>

Here is My Controller

public function actionAdd_quick()

{

    $model=new Question;
    $answers=new Answers;

    if(isset($_POST['Question'],$_POST['Answers']))
    {
        $model->attributes=$_POST['Question'];
        $answers->attributes=$_POST['Answers'];
                foreach ($model['questions'] as $value) {
                    $model->questions = $value;

                }
                $model->save();

            Yii::app()->user->setFlash('add_quick','Thank you for ');
            $this->refresh();

    }
    $this->render('add_quick',array('model'=>$model,'answers'=>$answers));
}

actually my process is to create multiple questions and answers ,but now getting error like

"Invalid argument supplied for foreach()" by using my code and how to send multiple questions and answers to mysql tables...

4

2 回答 2

2

改变这个

foreach ($model['questions'] as $value) {
                $model->questions = $value;

            }
            $model->save();

在这

foreach ($model['questions'] as $value) {
                $model->questions = $value;
                $model->save();
            }
于 2013-10-07T07:48:27.597 回答
0

尝试这个

 <?php echo $form->textField($model,'questions[]',array('id'=>"content_#index#_question textBox"));  ?>
于 2013-10-04T09:42:23.437 回答