亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在表 behlf 中插入數據

在表 behlf 中插入數據

PHP
鳳凰求蠱 2022-09-12 13:16:24
我正在嘗試通過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();

}

您應該在循環中保存答案。否則,將僅保存一行。


查看完整回答
反對 回復 2022-09-12
  • 1 回答
  • 0 關注
  • 83 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號