0

目前我正在尝试上传 CSV 文件并将每条记录逐个输入数据库。CSV 上的列与数据库中的字段名称相同,但有时数据在 CSV 中的顺序不同。当我以不同的顺序说时,我的意思是名称列表不是总是位于第一列,而是可能位于第三列。

我真正要问的是我将如何做上述事情,因为我真的被困住了。

目前我没有插入数据库,但它确实从 CSV 文件中获取了数组。

下面的代码:

索引.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>CSV Import</title>
</head>

<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="csv"/>
    <input type="submit" name="submit" value="Save" />
</form>

</body>

</html>

配置文件

<?php

/* Database Connection */

$con = mysql_connect('xxxxxxxx', 'xxxxxxxx', 'xxxxxxxx');

if(! $con )
{
  die('Could not connect: ' . mysql_error());
}

$select_db = mysql_select_db('xxxxxxxx');

?>

上传.php

<?php

include('config.php');

$file = "test.csv";
$separator = ",";
$length = filesize($file);

$handle = fopen($file, "r");
    $csvData = fgetcsv($handle, $length, $separator);
fclose($handle);

$i = 0;

while($i >= 1){

$title = $csvData[0];
$firstName = $csvData[1];
$secondName = $csvData[2];
$emailAddress = $csvData[3];
$houseNumber = $csvData[4];
$mobileNumber = $csvData[5];
$address1 = $csvData[6];
$address2 = $csvData[7];
$address3 = $csvData[8];
$address4 = $csvData[9];
$postcode = $csvData[10];

mysql_query("INSERT csv SET title='$title', firstName='$firstName' ,secondName='$secondName', emailAddress='$emailAddress', houseNumber='$houseNumber' ,mobileNumber='$mobileNumber', address1='$address1', address2='$address2', address3='$address3' ,address4='$address4', postcode='$postcode'")

$i++;

}

?>
4

2 回答 2

0

您用于插入记录的代码将在此处失败:

$i = 0;

while($i >= 1){   // $i = 0. This test will fail.

// do stuff

}
于 2013-07-08T21:19:56.850 回答
0

正确的 upload.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]];

    mysql_query("INSERT INTO csv (" . implode(',', array_keys($header)) . ") VALUES (" . implode(',', $values) . ")"); // only for demonstration - be careful with spaces and quotes in values - better switch to PDO!

}
fclose($handle);

希望有帮助!未经测试,但可能正在工作:)。

注意:当有人省略值并且您在数据库中将它们声明为NOT NULL. 请注意,此示例不会对 SQL 注入执行任何反措施。请忘记mysql_query并学习使用PDO

于 2013-07-08T21:43:20.393 回答