Page 1 of 1

Add records from csv

Posted: Tue Jul 04, 2006 12:30 am
by imatrox05
Hi i am using the following code to import records from a csv file into mysql

Code: Select all

connect_db();
$fname = $_FILES['userfile'];

 $fcontents = file ('./Productivity.txt'); 

//Displays Data from file
echo "<pre>";
print_r($arr);
echo "</pre>";


  for($i=0; $i<sizeof($fcontents); $i++) { 
	  $line = trim($fcontents[$i], '\t'); 
      $arr = explode("\t", $line); 

$sql = "insert into qcheck (qdate, empno, ename, jobno, activity, chapter, pages, time, dataerr, tagerr)values ('".implode("','", $arr) ."')"; 
$result = mysql_query ($sql) or die ("Query failed");
}
This code inports the data into the database but there is a minor glitch
The tab delimited data is set like this

Date orderno quantity team amount

When the print_r($arr) is executed it shows the data as it is

eg: 20-06-06 5585 50 TeamA 25000

But when the data is added into the database its added's a number 20 before the date and the data is stored like this

2020-06-06 5585 50 TeamA 25000

Can anyone throw light on why this is happening?

Posted: Tue Jul 04, 2006 12:38 am
by John Cartwright
I don't see anything specific that would be causing this. Can you put the print_r() within the loop, and can you print exactly what it outputs?

Posted: Tue Jul 04, 2006 1:01 am
by imatrox05

Code: Select all

Array
(
    [0] => 23-06-06
    [1] => 3080
    [2] => 50
    [3] => Team A
    [4] => 25000

)

Array
(
    [0] => 23-06-06
    [1] => 3081
    [2] => 75
    [3] => Team B
    [4] => 35000

)

Array
(
    [0] => 24-06-06
    [1] => 3083
    [2] => 100
    [3] => Team C
    [4] => 50000
)
And so on...
But in the database the date is stored like this

2023-06-06
2023-06-06
2024-06-06