我已经打开了与我的帖子相关的所有问题,但我仍然感到困惑。我尝试使用php导入我的csv文件。
我试试这个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($ data)结果是:
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($ data):
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) { }
。
答案 0 :(得分:0)
只是看了一眼,我想
if (!empty($_GET[success]))
应该是
if (!empty($_GET['success']))
答案 1 :(得分:0)
我认为这个问题就是你如何安排你的行动。当使用do while语句时,它将首先运行do中的代码,然后检查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()
中的代码。
希望这有帮助。
答案 2 :(得分:0)
按照您提供的print_r($ data)的输出,我可以看到这些字段看起来是分隔符。
您的代码在这里:
while ($data = fgetcsv($handle,1000,",","'")){
正在使用fgetcsv函数查找字段之间的标准逗号分隔符 - 这是常见做法,因为CSV代表逗号分隔值。
要让您的脚本使用制表符作为分隔符读取值,请尝试将以上行调整为:
while ($data = fgetcsv($handle,1000,"\t","'")){
希望这会有所帮助:)