SQL表仅导入第一列 [英] Sql table importing only first column

查看:100
本文介绍了SQL表仅导入第一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个csv文件上传到sql表中.这是我的代码:

I want to upload a csv file into a sql table. Here is my code:

<?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>";

}

?>

它成功导入,但仅在第一列:Famiglia(和id起作用).它们的其余部分没有值或为0.我要导入的文件包含数据.另外,在famiglia(即varchar 5)中,它从下一列获取第一个数字.因此,它不是biz,而是biz; 8(8是下一列的第一个数字).

It imports successfully, but ony the first column: Famiglia (and the id works). The rest of them have no value or 0 value. The file I want to import has data. Also, in famiglia, which is varchar 5, it takes the first number from the next column. So, instead of biz, it apperas biz;8 (8 being the first number from the next column).

有人知道问题可能在哪里吗?

Does anyone know where the problem could be?

谢谢!

推荐答案

我发现了问题所在.这是一个错误的定界符:,而不是;

I've found what the issue was. It was a wrong delimiter: , instead of ;

($ data = fgetcsv($ handle,1000,,")

($data = fgetcsv($handle, 1000, ",")

应该是

($ data = fgetcsv($ handle,1000,;")

($data = fgetcsv($handle, 1000, ";")

也许这对某人有帮助...

Maybe this helps somebody...

这篇关于SQL表仅导入第一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆