使用PHP将CSV文件上传到mysql数据库

Uploading CSV file into mysql database using PHP

我在将 CSV 文件上传到我的 MySQL 数据库时遇到问题。我不是一个非常有经验的编码人员,如果我试图解决问题的尝试已经结束,我很抱歉,但我将包括我迄今为止尝试过的内容。

这是代码的 HTML 部分:

1
2
3
4
5
<form enctype="multipart/form-data" action="camperUpload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" class="btn btn-border" />
</form>

这里是 PHP 部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function processFile($uploadedFile) {



    // file contents
    $file_contents = $uploadedFile["tmp_name"];

    $SQL_statement ="LOAD DATA LOCAL INFILE '$file_contents' INTO TABLE C_CAMPER FIELDS TERMINATED BY ','
                        LINES TERMINATED BY '\
' IGNORE 3 LINES
                        (camperFName,campLName,camperAddress,camperCity,camperZip,camperCountry,camperPhone,camperEmail,@camperAltEmail,@camperDOB,camperAge,camperSchool,camperGradYear,camperGPA,camperACT/SAT,camperPar,camperParPhone,camperPrimPos,camperSecPos);"
;
SET camperDOB = STR_TO_DATE(@camperDOB, '%b-%d-%Y'),
    camperAltEmail = NULLIF(@camperAltEmail, 'null');"
    // Run SQL query
    $DB->execute($SQL_statement);

CSV 文件的前两行并不重要,只有一行是标题,所以我让它跳过前三行。文件中的日期格式是 1999 年 6 月 5 日,所以我认为我必须尝试更改它。我把"LINES TERMINATED BY '\\
' IGNORE 3 LINES" 但我不确定我是否应该拥有它,这些行不会被任何东西终止。

我正在为我的数据库使用 phpMyAdmin,以防万一这很重要。

当我尝试执行此操作时,它只会转到一个页面,该页面显示该页面无法正常工作......所以我知道我可能已经离开或丢失了一些东西。任何帮助将不胜感激,谢谢!

编辑:
这是我的 PHP 文件,其中包含 Vasiliy Zverev 建议的更改。现在它不起作用,因为显然 phpMyAdmin 的 LOAD DATA LOCAL INFILE 命令有问题?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 <?php
    // version 1.02
    // display errors for debugging
    ini_set("display_errors", true);
    error_reporting(E_ALL);

    // Open a connection to the SQL server so we can run queries later.
    $conn = new mysqli(removed for privacy); // DON'T FORGET TO EDIT THIS PART!
    // Output error info if there was a connection problem
    if ($conn->connect_errno) {
        die(" Uh oh! It looks like we're having trouble connecting to the
 website at the moment. Try again soon! {$conn->connect_error}"
);
    }

    // file name
    $file_contents = $_FILES["uploadedfile"]["tmp_name"];

    $SQL_statement ="LOAD DATA LOCAL INFILE '$file_contents' INTO TABLE C_CAMPER FIELDS TERMINATED BY ','
                        LINES TERMINATED BY '\
' IGNORE 3 LINES
                        (camperFName,campLName,camperAddress,camperCity,camperZip,camperCountry,camperPhone,camperEmail,@camperAltEmail,@camperDOB,camperAge,camperSchool,camperGradYear,camperGPA,`camperACT/SAT`,camperPar,camperParPhone,camperPrimPos,camperSecPos)
                        SET camperDOB = STR_TO_DATE(@camperDOB, '%m/%d/%Y'),
                            camperAltEmail = NULLIF(@camperAltEmail, 'null');"
;
    // Run SQL query
    if( !$conn->query($SQL_statement)) {
        echo $conn->error;
    }

    // Close the SQL connection
    $conn->close();
?>


试试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
    // version 1.02
    // display errors for debugging
    ini_set("display_errors", true);
    error_reporting(E_ALL);

    // Open a connection to the SQL server so we can run queries later.
    $conn = new mysqli(removed for privacy); // DON'T FORGET TO EDIT THIS PART!
    // Output error info if there was a connection problem
    if ($conn->connect_errno) {
        die(" Uh oh! It looks like we're having trouble connecting to the
 website at the moment. Try again soon! {$conn->connect_error}"
);
    }

    // file name
    $file_contents = $_FILES["uploadedfile"]["tmp_name"];

    $SQL_statement ="LOAD DATA LOCAL INFILE '$file_contents' INTO TABLE C_CAMPER FIELDS TERMINATED BY ','
                        LINES TERMINATED BY '\
' IGNORE 3 LINES
                        (camperFName,campLName,camperAddress,camperCity,camperZip,camperCountry,camperPhone,camperEmail,@camperAltEmail,@camperDOB,camperAge,camperSchool,camperGradYear,camperGPA,`camperACT/SAT`,camperPar,camperParPhone,camperPrimPos,camperSecPos)
                        SET camperDOB = STR_TO_DATE(@camperDOB, '%m/%d/%Y'),
                            camperAltEmail = NULLIF(@camperAltEmail, 'null');"
;
    // Run SQL query
    if( !$conn->query($SQL_statement)) {
        echo $conn->error;
    }

    // Close the SQL connection
    $conn->close();
?>