我目前有一个 CSV,上传后应该由 upload.php 读取,然后将此数据上传到数据库。
目前我可以看到它正在从 CSV 文件中获取数组、正确的数据,并且它还试图将正确的数据插入到数据库中,但它似乎并没有最终将数据插入到数据库中。
这可能是什么?
下面的代码:
上传.php
<?php
include('config.php');
$file = "test.csv";
$separator = ",";
$length = 0; // size of the longest line(!), 0 = no limit
$fields = array('title', 'firstName', 'secondName', 'emailAddress', 'houseNumber', 'mobileNumber', 'address1', 'address2', 'address3', 'address4', 'postcode'); // use it as a white list
$handle = fopen($file, "r");
// get 1st line (header) and flip keys and values
// format like [title] --> 0, [firstName] --> 1, ...
$header = array_flip(fgetcsv($handle, $length, $separator));
$values = array();
// while we can read lines as csvData:
while(($csvData = fgetcsv($handle, $length, $separator)) !== false){
foreach ($fields as $field){ // put all values in an array in correct order
$values[] = $csvData[$header[$field]];
echo $field."<br>";
mysql_query("INSERT INTO csv (" . implode(',', array_keys($header)) . ") VALUES (" . implode(',', $values) . ")");
}
}
fclose($handle);
?>
提前致谢。