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条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘