在我的 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 格式不起作用,所以我在数据库中将其更改为 varchar 并且不需要使用 SET 我只是希望以后在我运行查询时它不会成为问题不同的日期并尝试计算其间的天数。
这是决赛:
<?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>
希望这可以帮助某人