如何使用For循环将插入数组的数据用于不同的行

时间:2017-10-30 08:48:21

标签: php mysql sql arrays database

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

$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 个答案:

答案 0 :(得分:1)

您可以使用. char连接插入数组值,并使用itselft连接$queryToInInsertInTable

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

注意SQL注入,检查数组值,它们不得来自客户端(GET或POST值),以防止SQL注入。请改用PDO mysql

答案 1 :(得分:1)

请检查用值

定义所有变量
<?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());
    }
}
?>

答案 2 :(得分:1)

你可以使用。用于连接以插入数组值。

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

}

答案 3 :(得分:0)

试试这个

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

那是因为你应该使用\“而不是',如果你使用的是phpmyadmin,它可能会给你错误