我正在尝试通过Id将数据插入数据库。我有三个问题,我有这些问题的答案。问题 ID 已正确插入表中。但是这些问题的答案 Id 未正确插入。表中只插入一个问题答案。让我解释一下这个函数:$post['SurveyAnswer']['question_id']有三个值。如果我们的结果就是。print_r( $post['SurveyAnswer']['question_id'])Array( [0] => 833 [1] => 834 [2] => 835)现在我有了这个问题的答案。和输出是。print_r($post['SurveyAnswer']['answer']);Array( [0] => text123 [1] => hello [2] => hello)我试图做的是将答案保存在数据库中,其中包含特定问题。我需要这样的结果。 [833] => text123 [834] => hello [835] => hello这是我的职能public function actionSaveAnswer(){ $post = \Yii::$app->request->post(); // echo "<pre>";print_r($post);die(); foreach ($post['SurveyAnswer']['question_id'] as $ques) { $post['SurveyAnswer']['answer']; $surveyAnswer = new SurveyAnswer(); $surveyAnswer->question_id = $ques; } if ($surveyAnswer->save()) { $flag = true; \Yii::$app->session->setFlash('success', \Yii::t('app', 'Your answers saved successfully.')); }}
1 回答
大话西游666
TA贡献1817条经验 获得超14个赞
我看不出你在哪里添加答案。如果您的答案和问题ID与您指示的匹配,请尝试以下操作:
foreach ($post['SurveyAnswer']['question_id'] as $id => $ques) {
$surveyAnswer = new SurveyAnswer();
$surveyAnswer->question_id = $ques;
$surveyAnswer->answer = $post['SurveyAnswer']['answer'][$id];
$surveyAnswer->save();
}
您应该在循环中保存答案。否则,将仅保存一行。
- 1 回答
- 0 关注
- 67 浏览
添加回答
举报
0/150
提交
取消