将csv文件导入到DB Rows中时是否进行了洗牌?

时间:2017-05-16 06:31:16

标签: php mysql csv fgetcsv

当我将csv文件导入到数据库中时,行被洗牌。请参阅图片

Shuffled db

我需要导入而不会改变行。我寻求解决方案超过一周。我无法解决这个问题。

任何人都可以帮忙解决这个问题吗?

<?php
    include 'connection1.php';                               
    $target_dir = dirname(__FILE__) . "/upload/";
    if (isset($_POST["import"]) && !empty($_FILES)) {
         $testid =$_POST['testidno'];
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $fileType = pathinfo($target_file, PATHINFO_EXTENSION);

        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {


            if (($getdata = fopen($target_file, "r")) !== FALSE) {
                fgetcsv($getdata);
                while (($data = fgetcsv($getdata)) !== FALSE) {
                    $fieldCount = count($data);
                    for ($c = 0; $c < $fieldCount; $c++) {
                        $columnData[$c] = $data[$c];
                    }
                    $subtopicid = mysqli_real_escape_string($con, $columnData[0]);
                    $subtopic = mysqli_real_escape_string($con, $columnData[1]);
                    $question = mysqli_real_escape_string($con, $columnData[2]);
                    $img_question = mysqli_real_escape_string($con, $columnData[3]);
                    $sub_question = mysqli_real_escape_string($con, $columnData[4]);                                                
                    $answer1 = mysqli_real_escape_string($con, $columnData[5]);
                    $answer2 = mysqli_real_escape_string($con, $columnData[6]);
                    $answer3 = mysqli_real_escape_string($con, $columnData[7]);
                    $answer4 = mysqli_real_escape_string($con, $columnData[8]);
                    $answer5 = mysqli_real_escape_string($con, $columnData[9]);
                    $correctanswer = mysqli_real_escape_string($con, $columnData[10]);
                    $solution = mysqli_real_escape_string($con, $columnData[11]);                                               
                    $setQ = mysqli_real_escape_string($con, $columnData[12]);
                    $topicname = mysqli_real_escape_string($con, $columnData[13]);
                    $import_data[] = "('" . $subtopicid . "','" . $subtopic . "','" . $testid . "','" . $_GET['id'] . "',
                                       '" . $question . "','" . $img_question . "','" . $sub_question . "', 
                                       '" . $answer1 . "','" . $answer2 . "','" . $answer3 . "','" . $answer4 . "', 
                                       '" . $answer5 . "','" . $correctanswer . "','" . $solution . "',
                                       '" . $setQ . "','" . $topicname . "')";



// SQL Query to insert data into DataBase
                }
                $import_data = implode(",", $import_data);                                  
                $query = "INSERT INTO advanced_questions (subtopicid,subtopic,testid,courseid,
                          question,img_question,sub_question,answer1,answer2,answer3,answer4,answer5,correctanswer, 
                          solution,setQ,topicname) VALUES  $import_data "; 

                $result = mysqli_query($con, $query);                            
                fclose($getdata);
            }
        }
    }
?>

0 个答案:

没有答案