Add records from csv

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imatrox05
Forum Newbie
Posts: 16
Joined: Wed Mar 29, 2006 8:44 pm

Add records from csv

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
imatrox05
Forum Newbie
Posts: 16
Joined: Wed Mar 29, 2006 8:44 pm

Post 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
Post Reply