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

如何使用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')";
}

我使用for循环将数据插入数据库, 我将数据存储在Array中以插入表中 (ex.- questionIdArray) 我在下面分享我的代码。 这种方法是正确的还是我应该改进它。 p>

  $ 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&lt; sizeof($ questionIdArray); $ i ++){
 
 // //将查询插入临时表
 $ 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')“; 
} 
  code>  pre> 
  div>

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());
    }
}
?>

try this

     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."\")"; }

that's because you should use \" instead of ', it may give you errors if you're using phpmyadmin

You can use the . char concatenation to insert array values, and to concat $queryToInInsertInTable with itselft :

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."')";
}

Caution with SQL injection, check the array values, they must not come from the client side (GET or POST values), to prevent SQL injection. Use PDO mysql instead

You can use the . for concatenation to insert array values.

for($i=0; $i < sizeof($questionArray); $i++){
//Insert Query To Temp Table
$queryToInsertInTable .= "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."')";

}