无法使用PHP将csv文件数据提交到相应的数据库字段中

时间:2013-07-08 21:10:31

标签: php csv sql-insert fgetcsv

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

我真正想问的是,如果我真的陷入困境,我将如何做到这一点。

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

以下代码:

的index.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>

的config.php

<?php

/* Database Connection */

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

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

$select_db = mysql_select_db('xxxxxxxx');

?>

upload.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++;

}

?>

2 个答案:

答案 0 :(得分:0)

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

$i = 0;

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

// do stuff

}

答案 1 :(得分: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