0

I here have a codes that inputs excel into my table Biometrics. Its working but i cant find a way to import it to other table which has only 3 fields. in my CSV I have 5 columns. I only want to get in my CSV 1 column and put it in my User_dummy table can you help me with

`

 mysql_select_db("hris_db",$link) or die ("Cannot select the database!");
// Set your CSV feed
$feed = 'excel/SampleLogs.csv';
//$uploadid = 2;
// Arrays we'll use later
$keys = array();
$newArray = array();

 // Function to convert CSV into associative array
function csvToArray($file, $delimiter) { 
  if (($handle = fopen($file, 'r')) !== FALSE) { 
    $i = 0; 
    while (($lineArray = fgetcsv($handle, 0, $delimiter, '"')) !== FALSE) { 
      for ($j = 0; $j < count($lineArray); $j++) { 
        $arr[$i][$j] = $lineArray[$j]; 
      } 
      $i++; 
    } 
    fclose($handle); 
  } 
  return $arr; 
} 
// Do it
$data = csvToArray($feed, ',');

// Set number of elements (minus 1 because we shift off the first row)
$count = count($data);

//Use first row for names  
$labels = array('empno','date_created','time_created','status','device');  

foreach ($labels as $label) {
  $keys[] = trim($label);
}


// Bring it all together
for ($j = 0; $j < $count; $j++) {
  $d = array_combine($keys, $data[$j]);
  $newArray[$j] = array_map('trim',$d);
}
//count number of rows in database
$q = "SELECT * FROM biometrics";
$res = mysql_query($q);
$numrows = mysql_num_rows($res);
$slicearray = array_slice($newArray,$numrows);
    echo $numrows;
    var_dump($slicearray);
    //$reverse = array_reverse($newArray,true);
    //var_dump($reverse);
    //$uniqueid = uniqid();
foreach($slicearray as $key=>$value){

       $implodearray = "'" . implode($value, "','") . "'";
       $keysString = implode(",", array_keys($value)); 
       $keylower = strtolower(str_replace(str_split(" '-/"),'_',$keysString));

        $sql = "INSERT INTO biometrics ($keylower)
                SELECT * FROM (SELECT '".$value['empno']."','".$value['date_created']."','".$value['time_created']."','".$value['status']."' as status,'".$value['device']."' as device) As tmp
                WHERE NOT EXISTS (SELECT $keylower FROM biometrics WHERE empno = '".$value['empno']."' AND date_created = '".$value['date_created']."' AND time_created = '".$value['time_created']."' AND status = '".$value['status']."' AND device = '".$value['device']."')";
       mysql_query($sql) or die(mysql_error());

       //echo $sql;

       //var_dump($keylower);
       //var_dump($value);

    }

?>`

Rolando Isidoro
  • 4,983
  • 2
  • 31
  • 43
Michael So
  • 73
  • 10
  • 1
    Have you considered using [`LOAD DATA INFILE`](http://dev.mysql.com/doc/en/load-data.html) or [mysqlimport](http://dev.mysql.com/doc/en/mysqlimport.html)? – eggyal Jul 23 '13 at 08:21
  • not yet, this set of codes was not made by me. It was given to me for me to edit. so I have no idea what is it really. I tried to use Load Data infile but I think im doing it wrong? I dont know, I have 3 fields in my MYSQL and I only want to get one for example. – Michael So Jul 23 '13 at 08:26
  • You can achieve that with dummy user variables in `LOAD DATA INFILE`'s `(col_name_or_user_var,...)` clause. Search the manual page for `dummy`. – eggyal Jul 23 '13 at 08:29
  • here you go.. http://stackoverflow.com/a/11077961/1042240 – Ahmed Z. Jul 23 '13 at 08:36

1 Answers1

0

yes this is very simple https://i.stack.imgur.com/dxthc.jpg

use this link image and you get idea how to import csv in database

or if you want to this by query then use it

LOAD DATA LOCAL INFILE '/your_file.csv' INTO TABLE your_table FIELDS TERMINATED BY ',' LINES TERMINATED BY ',' (field1, filed2, field3);

Rahul
  • 21
  • 3