2

I am trying to upload a csv file and save it in db. The problem is its returning Notice Error

Notice: Use of undefined constant csv - assumed 'csv' in D:\xampp\htdocs\nag\admin\process\upload_case.php on line 5

Notice: Use of undefined constant size - assumed 'size' in D:\xampp\htdocs\nag\admin\process\upload_case.php on line 5

Notice: Undefined index: csv in D:\xampp\htdocs\nag\admin\process\upload_case.php on line 5

I have stuck with it since 2 hours debugging, can some one please help fix.

Below is the code.

<?php    
include "../config/config.php";

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 { 
        if ($data[0]) { 
            mysql_query("INSERT INTO case_view (
                    courtname, casenumber, year, pfname, plname, 
                    cell, fhearing, cstatus, cdescri
                ) 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]) . "'
                )
            "); 
        } 
    } while ($data = fgetcsv($handle, 1000, ",", "'"));
    // 

    //redirect 
    header('Location: import.php?success=1'); die;
   // echo "Import Successfull";
}

?>

1
  • You may consider using PDO with prepared statements for performance reasons. Commented Apr 27, 2015 at 4:55

2 Answers 2

4

When you are using $_FILES[csv] without quotes it will assume that csv is a constant, so wrap it up with quotes and try again. Try with -

if ($_FILES['csv']['size'] > 0) { 
    .....
    $file = $_FILES['csv']['tmp_name']; 

Add a check for it -

if (!empty($_FILES['csv']['size']) && $_FILES['csv']['size'] > 0) { 
       ..............
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, by adding the Quotes 2 Notice error disappeared but still i get 1 notice error Notice: Undefined index: csv in D:\xampp\htdocs\nag\admin\process\upload_case.php on line 5
Now i do not see any error but its directly exiting the if loop
Add the form structure.
Ok, i had missed enctype=multipart/form-data now it works great and also save in db, but i get this notice error even though the data is inserted Notice: Undefined variable: data in D:\xampp\htdocs\nag\admin\process\upload_case.php on line 13
0

You have a syntax error. You open an if block on line 5, but never close it after your while loop.

1 Comment

Thanks, i closed the if loop, but i started getting notice error Notice: Use of undefined constant csv - assumed 'csv' in D:\xampp\htdocs\nag\admin\process\upload_case.php on line 5 Notice: Use of undefined constant size - assumed 'size' in D:\xampp\htdocs\nag\admin\process\upload_case.php on line 5 Notice: Undefined index: csv in D:\xampp\htdocs\nag\admin\process\upload_case.php on line 5

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.