0

我有一个 csv 文件,其中包含以下数据:

SUBJ_CODE 描述 时间 天 房间单位 EC-26765 FORLAN EL 9:30 - 10:30 AM MWF 203 3 EC-26773 IT 421 L 11:30 - 12:30 PM MWF CISC 1 EC-26781 IT 421 5:01 - 6: 31 PM TTH CISC 2 EC-26823 IT 420 8:30 - 11:30 AM SAT MA1 3 EC-26807 IT 423 3:00 - 4:30 PM TTH M1 3 EC-26799 IT 422 8:01 - 9:01 PM TTH 211 2 EC-26815 IT 422 L 6:31 - 8:01 PM TTH C1 1 ED-08326 FIL 38 7:30 - 9:00 AM TTH EDTE 3 IS-24331 MATH 16 12:00 - 1:30 PM TTH 203 3

我想将此 csv 文件保存到我的 phpadmin 中,如下所示: ID_NO SUBJ_CODE 描述 TIME DAYS ROOM UNITS 08329 EC-26765 FORLAN EL 9:30 - 10:30 AM MWF 203 3 08329 EC-26773 IT 421 L 11:30 - 12: 30 PM MWF CISC 1 08329 EC-26781 IT 421 5:01 - 6:31 PM TTH CISC 2 08329 EC-26823 IT 420 8:30 - 11:30 AM SAT MA1 3 08329 EC-26807 IT 423 3:00 - 4 :30 PM TTH M1 3 08329 EC-26799 IT 422 8:01 - 9:01 PM TTH 211 2 08329 EC-26815 IT 422 L 6:31 - 8:01 PM TTH C1 1 08329 ED-08326 FIL 38 7:30 - 上午 9:00 TTH EDTE 3 08329 IS-24331 数学 16 12:00 - 下午 1:30 TTH 203 3

这是我的代码:

    if(isset($_POST['submit']))     
    {

    if($_FILES["file"]["type"] != "application/vnd.ms-excel")
    {
        echo "This is not a CSV file.";
    }
    elseif(is_uploaded_file($_FILES['file']['tmp_name'])){


        //Process the CSV file
        $handle = fopen($_FILES['file']['tmp_name'], "r");
        $data = fgetcsv($handle, 10000, ","); //Remove if CSV file does not have column headings
        while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) 
        {

            //$att = $myrow['STUD_ID'];
            $att0 = mysql_real_escape_string($data[0]);
            $att1 = mysql_real_escape_string($data[1]);
            $att2 = mysql_real_escape_string($data[2]);
            $att3 = mysql_real_escape_string($data[3]);
            $att4 = mysql_real_escape_string($data[4]);
            $att5 = mysql_real_escape_string($data[5]);

            $sql = "INSERT INTO schedule (IDNO,SUBJ_CODE,DESCRIPTION,TIME,DAYS,ROOM,UNITS)
                                  VALUES ('$att','$att0','$att1','$att2','$att3','$att4','$att5')";
            mysql_query($sql);
        }
        mysql_close($evancon);
        echo "<script>alert('CSV file successfully imported.');</script>";
    }
    else{
        die("You shouldn't be here");
    }
    }
        ?>

希望你能回答这个问题....谢谢

4

1 回答 1

0

我假设的ID_NO列是主键,所以你不能有重复。通过您的示例数据的外观,您可以创建一个复合主键,ID_NO并且SUBJ_CODE它是唯一的。

于 2012-08-15T10:47:45.440 回答