0

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) { }.

7
  • 2
    "I try this tutorial. But I got this error" --- That's not a tutorial, it's code. Commented Apr 2, 2014 at 3:16
  • Is there a blank line in your csv? Have you tried a print_r($data); to see what it contains so you can troubleshoot the code? I'm guessing it's from an old tutorial too, as you really shouldn't use mysql_* functions which are obsolete in newer PHP versions, read up on using mysqli_* functions instead. There is also no escaping or sanatising of data which leaves this script wide open to sql injection. Commented Apr 2, 2014 at 3:19
  • Even if it were a tutorial, it uses the deprecated ext/mysql API. You should look for something better than that. Commented Apr 2, 2014 at 3:19
  • @Fred-ii-: that's a link file from some sites. Commented Apr 2, 2014 at 3:20
  • See this Q&A on SO => stackoverflow.com/q/21332380 it might help. @nunu Commented Apr 2, 2014 at 3:29

3 Answers 3

0

Just glancing through, I think

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

should be

 if (!empty($_GET['success'])) 
Sign up to request clarification or add additional context in comments.

Comments

0

The problem i think is how you placed your do while. when using a do while statement, it will first run the code inside the do, then checks the while statement.

What you should do, instead of

 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,",","'")); 

USE

 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])."'
            )
        ");
    }



 }

Its basically the same, it just execute the code in while() first before doing any of the code inside.

Hope this helps.

Comments

0

Going by the output of print_r($data) that you provided, I can see that the fields look to be tab separated.

Your code here:

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

is looking for the standard comma delimiter between fields using the [fgetcsv][1] function - which is common practise, as CSV stands for Comma Separated Values.

To get your script to read the values in using tabs as delimiters, try adjusting the above line to:

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

Hope this helps :) [1]: https://www.php.net/manual/en/function.fgetcsv.php

4 Comments

show PHP Notice: Undefined index: csv at line 1 of my php script.
line 1 = if ($_FILES["csv"]["size"] > 0) { .. which tells me it's not receiving the file upload from the form.
if not receiving any file, why after print_r($data) I got the result ?
try putting a var_dump($_FILES) at the top of your code, to see what $_FILES contains

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.