2012-06-21 54 views
0

我想上傳一個csv文件到sql表中。這裏是我的代碼:Sql表只導入第一列

<?php 

include "connection.php"; //Connect to Database 

$deleterecords = "TRUNCATE TABLE axioma_ordini"; //empty the table of its current records 
mysql_query($deleterecords); 

//Upload File 
if (isset($_POST['submit'])) { 
    if (is_uploaded_file($_FILES['filename']['tmp_name'])) { 
     echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>"; 
     echo "<h2>Displaying contents:</h2>"; 
     readfile($_FILES['filename']['tmp_name']); 
    } 

    //Import uploaded file to Database 
    $handle = fopen($_FILES['filename']['tmp_name'], "r"); 

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     $import="INSERT into axioma_ordini(`id`, `Famiglia`, `Cod Cliente`, `Ragione Sociale`, Articolo, Descrizione, Tipo, `Valore Euro`, Valuta, `Valore in Valuta`, Cambio, `Mese Competenza`, Anno, `Cir Medio`, Giorni, Satellite, `Satellite per Risorsa`, NumeroDocumento, `Data Doc`, `Nome Nave`, `Modem S/N`, `Burst Fwd`, `Burst Rtn`, Condivisione, `Cir Fwd`, `Cir Rtn`, `Cir Tot`, Business, Tecnologia,`Data Scadenza Ordine`, Network, Transponder, Nazione) values('','$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]','$data[14]','$data[15]','$data[16]','$data[17]','$data[18]','$data[19]','$data[20]','$data[21]','$data[22]','$data[23]','$data[24]','$data[25]','$data[26]','$data[27]','$data[28]','$data[29]','$data[30]','$data[31]')"; 

     mysql_query($import) or die(mysql_error()); 
    } 

    fclose($handle); 

    print "Import done"; 

    //view upload form 
}else { 

    print "Upload new csv by browsing to file and clicking on Upload<br />\n"; 

    print "<form enctype='multipart/form-data' action='upload.php' method='post'>"; 

    print "File name to import:<br />\n"; 

    print "<input size='50' type='file' name='filename'><br />\n"; 

    print "<input type='submit' name='submit' value='Upload'></form>"; 

} 

?> 

它成功導入,但ONY第一列:Famiglia(和ID的作品)。其餘的人沒有價值或0值。我想要導入的文件有數據。另外,在famiglia中,這是varchar 5,它將從下一列中獲取第一個數字。所以,而不是biz,它顯示biz; 8(8是下一列的第一個數字)。

有誰知道問題出在哪裏?

謝謝!

+0

你能附加幾行你的CSV? – dezso

回答

0

我發現問題所在。這是一個錯誤的分隔符:,而不是;

($數據= fgetcsv($處理,1000, 「」)

應該已經

($數據= fgetcsv($處理,1000, 「;」)

也許這有助於某人...