如何在CSV上载时跳过标题名称或列

时间:2013-02-10 17:06:43

标签: php mysql

在我的csv上传脚本中,csv文件中包含的标题或列名称数据总是相同的,但是相当重复,重复,空格,斜杠,井号,连字符等等。顺序列也总是一样的。

每当我使用这些csv文件中的一个使用上传脚本时,我都没有收到错误,也没有真正发生任何事情,没有数据进入数据库。但是,如果我更改标题或列名称一切正常。意思是我必须格式化csv文件的标题(列名)并删除所有空格,特殊字符等,一切似乎都没问题。

有没有办法只添加csv文件中的数据而不会妨碍标题或列名?

以下是我正在使用的内容:

    <?php

 include '_inc/include.php';
 if ($_FILES[csv][size] > 0) {

//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
fgets($handle);

//loop through the csv file and insert into database
do {
    if ($data[0]) {
        $mysql_query = mysql_query("INSERT INTO extrusiondata_01 (COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10, COL11, COL12, COL13, COL14, COL15, COL16, COL17, COL18, COL19, COL20, COL21, COL22, COL23, COL24, COL25, COL26, COL27, COL28, COL29, COL30, COL31, COL32, COL33, COL34, COL35, COL36, COL37, COL38, COL39, COL40, COL41, COL42, COL43, COL44, COL45, COL46, COL47, COL48, COL49, COL50, COL51, COL52, COL53, COL54, COL55, COL56, COL57, COL58, COL59, COL60, COL61, COL62, COL63, COL64, COL65, COL66, COL67, COL68, COL69, COL70, COL71, COL72, COL73, COL74, COL75, COL76, COL77, COL78, COL79, COL80, COL81, COL82, COL83, COL84, COL85, COL86, COL87, COL88, COL89, COL90, COL91, COL92, COL93, COL94, COL95, COL96, COL97, COL98, COL99, COL100, COL101, COL102, COL103, COL104, COL105, COL106, COL107, COL108, COL109, COL110, COL111, COL112, COL113, COL114, COL115, COL116, COL117, COL118, COL119, COL120, COL121, COL122, COL123, COL124, COL125, COL126) VALUES
            (
                '".addslashes($data[0])."',
                '".addslashes($data[1])."',
                '".addslashes($data[2])."',
                '".addslashes($data[3])."',
                '".addslashes($data[4])."',
                '".addslashes($data[5])."',
                '".addslashes($data[6])."',
                '".addslashes($data[7])."',
                '".addslashes($data[8])."',
                '".addslashes($data[9])."',
                '".addslashes($data[10])."',
                '".addslashes($data[11])."',
                '".addslashes($data[12])."',
                '".addslashes($data[13])."',
                '".addslashes($data[14])."',
                '".addslashes($data[15])."',
                '".addslashes($data[16])."',
                '".addslashes($data[17])."',
                '".addslashes($data[18])."',
                '".addslashes($data[19])."',
                '".addslashes($data[20])."',
                '".addslashes($data[21])."',
                '".addslashes($data[22])."',
                '".addslashes($data[23])."',
                '".addslashes($data[24])."',
                '".addslashes($data[25])."',
                '".addslashes($data[26])."',
                '".addslashes($data[27])."',
                '".addslashes($data[28])."',
                '".addslashes($data[29])."',
                '".addslashes($data[30])."',
                '".addslashes($data[31])."',
                '".addslashes($data[32])."',
                '".addslashes($data[33])."',
                '".addslashes($data[34])."',
                '".addslashes($data[35])."',
                '".addslashes($data[36])."',
                '".addslashes($data[37])."',
                '".addslashes($data[38])."',
                '".addslashes($data[39])."',
                '".addslashes($data[40])."',
                '".addslashes($data[41])."',
                '".addslashes($data[42])."',
                '".addslashes($data[43])."',
                '".addslashes($data[44])."',
                '".addslashes($data[45])."',
                '".addslashes($data[46])."',
                '".addslashes($data[47])."',
                '".addslashes($data[48])."',
                '".addslashes($data[49])."',
                '".addslashes($data[50])."',
                '".addslashes($data[51])."',
                '".addslashes($data[52])."',
                '".addslashes($data[53])."',
                '".addslashes($data[54])."',
                '".addslashes($data[55])."',
                '".addslashes($data[56])."',
                '".addslashes($data[57])."',
                '".addslashes($data[58])."',
                '".addslashes($data[59])."',
                '".addslashes($data[60])."',
                '".addslashes($data[61])."',
                '".addslashes($data[62])."',
                '".addslashes($data[63])."',
                '".addslashes($data[64])."',
                '".addslashes($data[65])."',
                '".addslashes($data[66])."',
                '".addslashes($data[67])."',
                '".addslashes($data[68])."',
                '".addslashes($data[69])."',
                '".addslashes($data[70])."',
                '".addslashes($data[71])."',
                '".addslashes($data[72])."',
                '".addslashes($data[73])."',
                '".addslashes($data[74])."',
                '".addslashes($data[75])."',
                '".addslashes($data[76])."',
                '".addslashes($data[77])."',
                '".addslashes($data[78])."',
                '".addslashes($data[79])."',
                '".addslashes($data[80])."',
                '".addslashes($data[81])."',
                '".addslashes($data[82])."',
                '".addslashes($data[83])."',
                '".addslashes($data[84])."',
                '".addslashes($data[85])."',
                '".addslashes($data[86])."',
                '".addslashes($data[87])."',
                '".addslashes($data[88])."',
                '".addslashes($data[89])."',
                '".addslashes($data[90])."',
                '".addslashes($data[91])."',
                '".addslashes($data[92])."',
                '".addslashes($data[93])."',
                '".addslashes($data[94])."',
                '".addslashes($data[95])."',
                '".addslashes($data[96])."',
                '".addslashes($data[97])."',
                '".addslashes($data[98])."',
                '".addslashes($data[99])."',
                '".addslashes($data[100])."',
                '".addslashes($data[101])."',
                '".addslashes($data[102])."',
                '".addslashes($data[103])."',
                '".addslashes($data[104])."',
                '".addslashes($data[105])."',
                '".addslashes($data[106])."',
                '".addslashes($data[107])."',
                '".addslashes($data[108])."',
                '".addslashes($data[109])."',
                '".addslashes($data[110])."',
                '".addslashes($data[111])."',
                '".addslashes($data[112])."',
                '".addslashes($data[113])."',
                '".addslashes($data[114])."',
                '".addslashes($data[115])."',
                '".addslashes($data[116])."',
                '".addslashes($data[117])."',
                '".addslashes($data[118])."',
                '".addslashes($data[119])."',
                '".addslashes($data[120])."',
                '".addslashes($data[121])."',
                '".addslashes($data[122])."',
                '".addslashes($data[123])."',
                '".addslashes($data[124])."',
                '".addslashes($data[125])."'
            )
        ");
    }
} while ($data = fgetcsv($handle,100000,",","'"));
//

//redirect


}

?>

<!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 http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import a CSV File with PHP & MySQL</title>
</head>

<body>

<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported by import 55.</b><br><br>"; } //generic success notice ?>

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  Choose your file: <br />
  <input name="csv" type="file" id="csv" />
  <input type="submit" name="Submit" value="Submit" />
</form>
    <P> Display Data Contents <a href ='Data-Display-test.php'>here</a></P>
</body>
</html>

我知道这在sql注入或安全方面可能不是最好的,所以任何建议都会很棒。我还没有那么远。

提前致谢

好的,我已经回到了LOAD DATA INFILE,但我遇到了问题。请原谅我没有可靠的参考来正确看待这项工作,也没有任何成功让它工作。我从未听说过fseek,或者看过它的用途。根据我的阅读,我认为fseek可能是我想要完成的。我只想将数据上传到数据库。你有一个fseek工作的例子吗?

我已尝试过LOAD DATA INFILE但无法确定错误的位置

        if ($data[0]) {
    LOAD DATA INFILE '$file' INTO TABLE 'tabledata_01' (COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10, COL11, COL12, COL13, COL14) VALUES
    $mysql_query = mysql_query("INSERT INTO tabledata_01 (COL1, COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10, COL11, COL12, COL13, COL14) VALUES
            (
                '".addslashes($data[0])."',
                '".addslashes($data[1])."',

我已将旧查询留在LOAD DATA INFILE下面并尝试查看我收到错误的原因或位置。如果你有一个fseek的例子,这可能是一个更好的解决方案,从我读到的内容看起来肯定更快。谢谢

由于我的托管,我能够使用LOCAL,我使用@VAR跳过不需要的字段/列。使用MULTIPLE SET是一件痛苦的事,但我确实弄明白了。

$sqlstatement="LOAD DATA LOCAL INFILE '$temp' INTO TABLE 000_datetest FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (id, @sdate, name, @anotherdate, color, 1moredate) SET anotherdate = IF(LENGTH(@anotherdate)=7,STR_TO_DATE(@anotherdate,'%m/%d/%Y'),STR_TO_DATE(@anotherdate,'%m/%d/%y')), sdate = IF(LENGTH(@sdate)=7,STR_TO_DATE(@sdate,'%m/%d/%Y'),STR_TO_DATE(@sdate,'%m/%d/%y'))";

我无法弄清楚为什么DATE格式不起作用,所以我在db中将其更改为varchar并且不需要使用SET我希望以后当II运行时它不会成​​为问题查询不同的日期并尝试计算两者之间的天数。

这是决赛:

<?php
if (isset($_POST['load']))
{
include '_inc/include.php';
$temp = $_FILES['myfile']['tmp_name'];
$sqlstatement="LOAD DATA LOCAL INFILE '$temp' INTO TABLE 000_datetest FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES(@COL1, @COL2, @COL3, @COL4, @COL5, @COL6, @COL7, @COL8, @COL9, @COL10, @COL11, @COL12, @COL13, listdate, closedate, contractdate, @COL17, status, statuschangedate, withdrawdate, canceldate, @COL22, @COL23, @COL24, listprice, soldprice, @COL27, @COL28, @COL29, @COL30, @COL31, @COL32, @COL33, @COL34, @COL35, @COL36, @COL37, @COL38, @COL39, @COL40, @COL41, @COL42, @COL43, @COL44, @COL45, @COL46, @COL47, @COL48, @COL49, @COL50, @COL51, gla, @COL53, @COL54, @COL55, built, @COL57, @COL58, @COL59, @COL60, @COL61, @COL62, @COL63, @COL64, @COL65, @COL66, @COL67, @COL68, @COL69, @COL70, @COL71, @COL72, @COL73, @COL74, @COL75, @COL76, @COL77, @COL78, @COL79, @COL80, @COL81, @COL82, owner, @COL84, @COL85, @COL86, @COL87, @COL88, @COL89, @COL90, @COL91, @COL92, @COL93, @COL94, fore, @COL96, @COL97, shortsale, @COL99, @COL100, @COL101, @COL102, @COL103, @COL104, @COL105, @COL106, @COL107, @COL108, @COL109, @COL110, @COL111, @COL112, @COL113, @COL114, @COL115, @COL116, @COL117, @COL118, @COL119, @COL120, @COL121, @COL122, @COL123, @COL124, @COL125, @features)";
mysql_query($sqlstatement) or die(mysql_error());
echo "It worked";
echo "<p><a href='upload-display2.php'>go to page</a></p>";
} 

?>

<form method="post" action="upload2.php" enctype="multipart/form-data">
<input name="myfile" type="file" />
<input name="load" type="submit" value="submit" /></form>

希望这有助于某人

2 个答案:

答案 0 :(得分:0)

为什么不使用LOAD DATA INFILE

但是,要跳过标题行,您可以使用fseek

答案 1 :(得分:0)

使用循环。

<?php

$row = 0;
while (($values = fgetcsv($handle)) !== false) {
    $row++;
    if ($row === 1) {
        $keys = $values;
        continue;
    }
    $row = array_combine($keys, $values);
}

这将使用第一行作为标题,然后组合任何后续行的值,以提供标题名称与单元格值的关联数组。