duanhuang3074 2017-10-30 08:48
浏览 49
已采纳

如何使用For循环将插入数组的数据插入不同的行[关闭]

I am inserting data to the database using a for Loop, I am having data stored in Array to insert into table (ex.- questionIdArray) I am sharing my code below. Is this approach correct or should I refine this.

$retriveDataForJSON= mysql_query($querytoGetUsageOfSelectStudents);
while ($row = mysql_fetch_array($retriveDataForJSON))
{
    $USER_ID= $row["USER_ID"];                          
    $SESSION_ID= $row["SESSION_ID"];
    $QUESTION_IDS = $row["QUESTION_IDS"];
    $questionIdArray=explode(',',$QUESTION_IDS);
    $RESPONSES= $row["RESPONSES"];
    $responseArray=explode('|',$RESPONSES);
    $RIGHT_OR_WRONG = $row["RIGHT_OR_WRONG"];
    $rightWrongArray=explode('|',$RIGHT_OR_WRONG);
    $TIME_PER_QUESTION = $row["TIME_PER_QUESTION"];
    $timePerQuestionArray=explode('|',$TIME_PER_QUESTION);
    $QUIZ_SIZE = $row["QUIZ_SIZE"];
    $CORRECT_ANSWERS = $row["CORRECT_ANSWERS"];
    $COURSE_ID= $row["COURSE_ID"];
    $TOPIC_NAME = $row["TOPIC_NAME"];
    $SUBTOPIC_IDS = $row["SUBTOPIC_IDS"];
    //$subtopicNameArray = getSubtopicNameArray(subtopicIds)
    //$topicNameArray = getTopicNameArray(subtopicIds)
    $START_TIME = $row["START_TIME"];
    $END_TIME = $row["END_TIME"];
    $TIME_TAKEN = $row["TIME_TAKEN"];   
 for($i=0;$i<sizeof($questionIdArray);$i++){

        //Insert Query To Temp Table
   $queryToInInsertInTable= "INSERT INTO `temp_assessment_data`(`Q_ID`, `RESPONCE`, `W/R/B`, `TIME_TAKEN`, `USER_ID`, `TOPIC`, `SUBTOPIC`, `CLASS`, `SCHOOL`, `BLOCK`, `DISTRICT`) VALUES
    ('$questionIdArray[$i]','$responseArray[$i]','$rightWrongArray[$i]','$timePerQuestionArray[$i]','$USER_ID','$className','$schoolName','$blockName','$District')";
}
  • 写回答

4条回答 默认 最新

  • doujimiao7480 2017-10-30 09:29
    关注

    please check define all variable with value

    <?php
    $retriveDataForJSON= mysql_query($querytoGetUsageOfSelectStudents);
    while ($row = mysql_fetch_array($retriveDataForJSON))
    {
        $USER_ID= $row["USER_ID"];                          
        $SESSION_ID= $row["SESSION_ID"];
        $QUESTION_IDS = $row["QUESTION_IDS"];
        $questionIdArray=explode(',',$QUESTION_IDS);
        $RESPONSES= $row["RESPONSES"];
        $responseArray=explode('|',$RESPONSES);
        $RIGHT_OR_WRONG = $row["RIGHT_OR_WRONG"];
        $rightWrongArray=explode('|',$RIGHT_OR_WRONG);
        $TIME_PER_QUESTION = $row["TIME_PER_QUESTION"];
        $timePerQuestionArray=explode('|',$TIME_PER_QUESTION);
        $QUIZ_SIZE = $row["QUIZ_SIZE"];
        $CORRECT_ANSWERS = $row["CORRECT_ANSWERS"];
        $COURSE_ID= $row["COURSE_ID"];
        $TOPIC_NAME = $row["TOPIC_NAME"];
        $SUBTOPIC_IDS = $row["SUBTOPIC_IDS"];
        //$subtopicNameArray = getSubtopicNameArray(subtopicIds)
        //$topicNameArray = getTopicNameArray(subtopicIds)
        $START_TIME = $row["START_TIME"];
        $END_TIME = $row["END_TIME"];
        $TIME_TAKEN = $row["TIME_TAKEN"];   
         for($i=0;$i<sizeof($questionIdArray);$i++){
    
                //Insert Query To Temp Table
             $queryToInInsertInTable = "insert into `temp_assessment_data` set 
                                `Q_ID`      = '".$questionIdArray[$i]."',
                                `RESPONCE`  = '".$responseArray[$i]."',
                                `W/R/B`     = '".$rightWrongArray[$i]."',
                                `TIME_TAKEN`= '".$timePerQuestionArray[$i]."',
                                `USER_ID`   = '".$USER_ID."',
                                `TOPIC`     = '".$TOPIC_NAME."',
                                `SUBTOPIC`  = '".$SUBTOPIC_IDS."',
                                `CLASS`     = '".$className."',
                                `SCHOOL`    = '".$schoolName."',
                                `BLOCK`     = '".$blockName."',
                                `DISTRICT`  = '".$District."
                                ";
    
            mysql_query($queryToInInsertInTable) or die(mysql_error());
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效