Q: how to import data from text file into MYSQL database table? i am trying to import data from a text file into my mysql database table. Some how data is imported but its not the correct data. Also i get this error:
Undefined offset: 6 in C:\wamp\www\Upload\main.php on line 36
Please review my code.
Below is my code:
<?php
    $con=mysqli_connect("localhost","root","","hiren");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }


    $f = fopen("./uploads/imp.txt", "r");
    while(!feof($f)) { 
      $data = explode(" ", fgets($f));

      $id =$data[0];
       $emp_id = $data[1];
       $date = $data[2];
       $abc = $data[3];
       $def = $data[4];
       $entry = $data[5];
       $ghi = $data[6];

       mysqli_query($con,"INSERT INTO `daily_data2` (emp_id, date, abc, def, entry, ghi) 
    VALUES ('$emp_id', '$date', '$abc', '$def', '$entry', '$ghi')") or die(mysql_error());

        }

        fclose($f);

        ?>

My Table :

The Imp.txt file:

the Data is inserted in this manner which is wrong :]
The error i am getting:


A: ok i guess this will solve your problem
$handle = fopen("./uploads/imp.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {

         $lineArr = explode("\t", "$line");
         var_dump($lineArr); // to make sure array is ok

         // instead assigning one by onb use php list -> http://php.net/manual/en/function.list.php            
         list($emp_id, $date, $abc, $def, $entry, $ghi) = $lineArr;

         // and then insert data
         mysqli_query($con,"INSERT INTO `daily_data2` (emp_id, date, abc, def, entry, ghi) 
VALUES ('$emp_id', '$date', '$abc', '$def', '$entry', '$ghi')");
    }

    fclose($handle);
}


A: The error shows like, for some row, there is no data available in the 6th index which you should pass the null string like the below code.
Note: Hope each rows splitted using " " by which you are trying to explode but this is not a good practice because if any of the data contains space which also considers as a new row. 
Update your code like this:
   $data = explode(" ", fgets($f));

   $id = !empty($data[0]) ? $data[0] : '';
   $emp_id = !empty($data[1]) ?$data[1] : '';
   $date = !empty($data[2]) ? $data[2] : '';
   $abc = !empty($data[3]) ? $data[3] : '';
   $def = !empty($data[4]) ? $data[4] : '';
   $entry = !empty($data[5]) ? $data[5] : '';
   $ghi = !empty($data[6]) ? $data[6] : '';


A: you have to index more fields than they get as a result
You index  7 fields but you have only 6 fields
 $data = explode(" ", fgets($f));   // this give you 6 value but you index 7

 $id =$data[0];
   $emp_id = $data[1];
   $date = $data[2];
   $abc = $data[3];
   $def = $data[4];
   $entry = $data[5];
   $ghi = $data[6];

   mysqli_query($con,"INSERT INTO `daily_data2` (emp_id, date, abc, def, entry, ghi) 
VALUES ('$emp_id', '$date', '$abc', '$def', '$entry', '$ghi')") or die(mysql_error());


A: string fgets ( resource $handle [, int $length ] )
Reading ends when length - 1 bytes have been read, or a newline (which is included in the return value), or an EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line.
so use:
$data = explode(" ", trim(fgets($f,4096)));
also use 
ini_set("auto_detect_line_endings", true);
before fopen
