0

hello all i am having very unusuall issue here my code is below

 if (isset($_POST['csvSubmit'])) {

$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');

if(in_array($_FILES['csv']['type'],$mimes)){    


$file = $_FILES['csv']['tmp_name']; 
$handle = fopen($file,"r"); 

$count=0;    
while (($data = fgetcsv($handle, 100000, ',')) !== FALSE) {

    if($count>0){

        mysqli_query($connection,"INSERT INTO `csv_sheet` (`cscid`, `csfname`, `cslname`, `csemail`, `csphone`, `csdep`, `csgender`, `csdob`, `csdate`, `csip`) VALUES ('0', '$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]')");

    }

    $count++;
} 
    print "File Uploded Successfully";

fclose($handle);


}
else{
    die("Sorry, This file type not allowed");
}
}     

Error

 Notice: Undefined offset: 7 in     C:\xampp\htdocs\site\action\csv_import.php on line 45

 Notice: Undefined offset: 8 in    C:\xampp\htdocs\site\action\csv_import.php on line 45
 File Uploded Successfully

while the file is imported sucessfully but i get to see this error every time please suggest me what to do here and also help me to understand the code

Maximus2012
  • 1,799
  • 2
  • 12
  • 15
sonam Sharma
  • 548
  • 12
  • 30
  • Which one is line 45 ? – Maximus2012 Jan 07 '16 at 21:27
  • Is it the upload part that is giving issues or the DB insert part or both ? – Maximus2012 Jan 07 '16 at 21:28
  • at line 45 the query is there.. – sonam Sharma Jan 07 '16 at 21:35
  • 1
    In that case, most likely the `$data` array does not have values at indices 7 & 8 so `$data[7]` and `$data[8]` do not exist. You will have to re-write the query to conditionally check for the existence of these values using something like `isset()` or change the query statement to exclude these fields. It depends on the structures of your database and CSV file. – Maximus2012 Jan 07 '16 at 21:39
  • 1
    You can conditionally build a MySQL query using `isset()` and `INSERT INTO SET` syntax for the query statement. http://dev.mysql.com/doc/refman/5.7/en/insert.html http://stackoverflow.com/questions/861722/mysql-insert-into-table-values-vs-insert-into-table-set – Maximus2012 Jan 07 '16 at 21:41

0 Answers0