I already open all question which related my post, but I'm still confuse. I try to import my csv file using php.
I try this tutorial. But I got this error :
PHP Notice: Undefined offset: 1
PHP Notice: Undefined offset: 2
PHP Notice: Undefined offset: 3
PHP Notice: Undefined index: csv
I've modify to my database:
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) result is:
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 )
EDIT
change while ($data = fgetcsv($handle,1000,",","'")){ become while ($data = fgetcsv($handle,1000,"\t","'")){.Then 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 )
file still not uploaded, show :PHP Notice: Undefined index: csv. Then put var_dump like :
var_dump($_FILES);
if ($_FILES["csv"]["size"] > 0) {
I got array(0) { }.