2014-04-02 13 views
0

我已經打開這關係我的文章的所有問題,但我仍然混淆。我嘗試使用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) { }

+2

*「我試試這個教程,但我得到了。這個錯誤「* ---這不是教程,它是代碼。 –

+0

您的csv中是否有空行?你是否嘗試過print_r($ data);看看它包含什麼,這樣你可以解決代碼故障?我猜它也來自舊教程,因爲你真的不應該使用在新PHP版本中過時的mysql_ *函數,而應該使用mysqli_ *函數進行閱讀。也沒有逃脫或sanatising的數據,這使腳本開放sql注入。 – flauntster

+0

即使它是一個教程,它也會使用已棄用的ext/mysql API。你應該尋找比這更好的東西。 – Mike

回答

0

只需一眼通過,我覺得

if (!empty($_GET[success])) 

應該

if (!empty($_GET['success'])) 
0

我認爲這個問題是你如何放置你做一段時間。使用DO while語句時,它會首先運行裏面做的代碼,然後檢查while語句。

你應該做的,而不是

do { 
    if ($data[0]) { 
     mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES 
      (
       '".addslashes($data[0])."', 
       '".addslashes($data[1])."', 
       '".addslashes($data[2])."' 
      ) 
     "); 
    } 
} while ($data = fgetcsv($handle,1000,",","'")); 

使用

while($data = fgetcsv($handle,1000,",","'")){ 
     if ($data[0]) { 
     mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES 
      (
       '".addslashes($data[0])."', 
       '".addslashes($data[1])."', 
       '".addslashes($data[2])."' 
      ) 
     "); 
    } 



} 

它基本上是相同的,只是在while()首先做任何代碼的內部之前執行的代碼。

希望這會有所幫助。

0

通過print_r的輸出去($數據),您提供的,我可以看到田野看起來是製表符分隔。

您的代碼在這裏:

while ($data = fgetcsv($handle,1000,",","'")){ 

正在使用fgetcsv功能領域之間的標準逗號分隔符 - 這是常見的做法,爲CSV代表逗號分隔值。

爲了讓您的腳本中使用標籤作爲分隔符來讀取值,試着調整上述線路:

while ($data = fgetcsv($handle,1000,"\t","'")){ 

希望這有助於:)

+0

show'PHP Notice:未定義索引:csv'在我的php腳本的第1行。 – nunu

+0

line 1 = if($ _FILES [「csv」] [「size」]> 0){..它告訴我它沒有從窗體接收文件上傳。 – flauntster

+0

如果沒有收到任何文件,爲什麼在print_r($ data)後得到結果? – nunu