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

2
  • What you want to do ? Insert multiple answers to a question. Or insert question and answer pairs Commented Oct 3, 2013 at 13:11
  • i have to insert both multiple Questions and answers,but here i mentioned only for question Commented Oct 4, 2013 at 6:08

2 Answers 2

2

change this

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

            }
            $model->save();

on this

foreach ($model['questions'] as $value) {
                $model->questions = $value;
                $model->save();
            }
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

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

1 Comment

Hi thanks for your response,i did change as you said but now single row only inserted...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.