在我的csv上傳腳本中,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的例子,這可能是一個更好的解決方案,它肯定看起來比我讀的更快。謝謝
我能夠使用本地,因爲我的託管,我用@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'))";
我無法弄清楚,爲什麼日期格式是行不通的,所以我改變了它在分貝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>
希望這可以幫助別人
我試過使用LOAD DATA INFILE,因爲這裏有幾處提到了其他文章中的幾個例子,但我無法讓它工作。我對fseek一無所知。我發現的唯一的其他事情是fastercsv,但我沒有使用Ruby。 – 2013-02-10 20:56:25
經過多次閱讀和學習後,我確定了LOAD DATA INFILE。我的主要問題是理解爲什麼我需要在那裏使用LOCAL,因爲我的主機。你們中的以下人員: – 2013-02-12 17:12:41
我能夠獲得多個SET使用,並且能夠讓我所需的所有字段都跳過。 – 2013-02-18 12:43:41