我已經打開這關係我的文章的所有問題,但我仍然混淆。我嘗試使用PHP導入我的csv文件。PHP公告:未定義的偏移數組文件不能上傳
我試試這個tutorial。但我得到這個錯誤:
PHP Notice: Undefined offset: 1
PHP Notice: Undefined offset: 2
PHP Notice: Undefined offset: 3
PHP Notice: Undefined index: csv
我已經修改到我的數據庫:
if ($_FILES["csv"]["size"] > 0) {
//get the csv file
$file = $_FILES["csv"]["tmp_name"];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
// do {
while ($data = fgetcsv($handle,1000,",","'")){
print_r($data);
if ($data[0]) {
mysql_query("INSERT INTO prod_schedule (RecNo, Model_Name, Lot_Number, Start_Serial,
Qty, Lot_Qty, Line, Line_Name, Start_Date, End_Date, Due_Date
) 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])."'
)
");
}
}
// while ($data = fgetcsv($handle,1000,",","'"));
//
//redirect
header('Location: upload.php?success=1'); die;
}
的print_r($數據)的結果是:
Array ([0] => RecNo Model_Name Lot_Number Start_Serial Qty Lot_Qty Line Line_Name Start_Date End_Date Due_Date) Array ([0] => 1 KD-R746BTU9D 011A 421 30 80 N0001 MA 01 3/3/2014 3/3/2014 12/31/2999) Array ([0] => 2 KW-XG56T2UD 057A 25081 79 440 N0001 MA 01 3/3/2014 3/3/2014 12/31/2999) Array ([0] => 3 KD-R646U9D 012B 561 1 60 N0001 MA 01 3/3/2014 3/3/2014 12/31/2999)
編輯
變化while ($data = fgetcsv($handle,1000,",","'")){
成爲while ($data = fgetcsv($handle,1000,"\t","'")){
。後來的print_r($數據):
array(1) { ["csv"]=> array(5) { ["name"]=> string(17) "prod_schedule.csv" ["type"]=> string(8) "text/csv" ["tmp_name"]=> string(14) "/tmp/phpb1ji8u" ["error"]=> int(0) ["size"]=> int(1943264) } }
Array ([0] => RecNo [1] => Model_Name [2] => Lot_Number [3] => Start_Serial [4] => Qty [5] => Lot_Qty [6] => Line [7] => Line_Name [8] => Start_Date [9] => End_Date [10] => Due_Date)
Array ([0] => 1 [1] => KD-R746BTU9D [2] => 011A [3] => 421 [4] => 30 [5] => 80 [6] => N0001 [7] => MA 01 [8] => 3/3/2014 [9] => 3/3/2014 [10] => 12/31/2999)
Array ([0] => 2 [1] => KW-XG56T2UD [2] => 057A [3] => 25081 [4] => 79 [5] => 440 [6] => N0001 [7] => MA 01 [8] => 3/3/2014 [9] => 3/3/2014 [10] => 12/31/2999)
Array ([0] => 3 [1] => KD-R646U9D [2] => 012B [3] => 561 [4] => 1 [5] => 60 [6] => N0001 [7] => MA 01 [8] => 3/3/2014 [9] => 3/3/2014 [10] => 12/31/2999)
文件還沒有上傳,顯示:PHP Notice: Undefined index: csv
。然後把var_dump
喜歡:
var_dump($_FILES);
if ($_FILES["csv"]["size"] > 0) {
我array(0) { }
。
*「我試試這個教程,但我得到了。這個錯誤「* ---這不是教程,它是代碼。 –
您的csv中是否有空行?你是否嘗試過print_r($ data);看看它包含什麼,這樣你可以解決代碼故障?我猜它也來自舊教程,因爲你真的不應該使用在新PHP版本中過時的mysql_ *函數,而應該使用mysqli_ *函數進行閱讀。也沒有逃脫或sanatising的數據,這使腳本開放sql注入。 – flauntster
即使它是一個教程,它也會使用已棄用的ext/mysql API。你應該尋找比這更好的東西。 – Mike