PHP/MySQL insert date inserts 0000-00-00

Hi folks I have the values from a .csv file being inserted into a mysql DB.

the csv looks like this:

04/29/2004,Ali,Islah,Alameda City,Quality Standards
04/29/2004,Dave,James,Alameda City,Quality Standards
04/29/2004,Lisa,Gibons,Alameda City,Quality Standards

The code looks like this:

      $conn = @mysql_connect($host, $user, $pw, $dbname);

if (!$conn) {
        echo "Connection failed\n";
$filename = "/web/pages/scripts/" . $csv_file_name ;//. ".csv";
$fcontents = file($filename);
   while (list($line_num, $line) = each($fcontents))
      $msg = $line;

      $CSVRecord = explode (",", $msg);

      $StringLength = strlen($CSVRecord[4])-1;
        $CSVInsert="INSERT into certifications(seminar_date,last_name,first_name,agency,seminar)
                  Values('$CSVRecord[0]', trim('$CSVRecord[1]'), trim('$CSVRecord[2]'), trim('$CSVRecord[3]'), trim('$CSVRecord[4]'));";
                        $result = mysql_query($CSVInsert) or die ("Error in query: $query. " . mysql_error());
      if(!$CSVInsert) echo("Invalid Insert");


It's pretty straight forward and works except the date. When the seminar_date field is of type varchar it inserts fine. But when the seminar_date field is type datetime, as it should be, it inserts as 0000-00-00

I'm porting this app from postgresql to mysql. Any help would be greatly appreciated.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi ewarmour,

You can insert the entire file into mysql in one statement without having to parse the data in php.


Muhammad WasifCommented:
try this query

$CSVInsert="INSERT into certifications(seminar_date,last_name,first_name,agency,seminar)
                  Values('".str_replace("/","-",$CSVRecord[0])."', trim('$CSVRecord[1]'), trim('$CSVRecord[2]'), trim('$CSVRecord[3]'), trim('$CSVRecord[4]'))";
ewarmourAuthor Commented:
Thanks wasifg, I tried that and the value is still 0000-00-00 00:00:00.

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

ewarmourAuthor Commented:
Hi steve918... no luck with the import

I've tried

mysql_query("LOAD DATA INFILE '$csv_file_name' INTO TABLE certifications FIELDS TERMINATED BY ','")
                    or die
                  ("Error in query: $query. " . mysql_error());

Muhammad WasifCommented:
The problem is in the format of the data i.e mm/dd/yyyy

04/29/2004,Ali,Islah,Alameda City,Quality Standards

but mysql accepts yyyy-mm-dd
 so change your query as follows

list($month, $day, $year) = explode("/",$CSVRecord[0]);

$CSVInsert="INSERT into certifications(seminar_date,last_name,first_name,agency,seminar)
                  Values('$year-$month-$day', trim('$CSVRecord[1]'), trim('$CSVRecord[2]'), trim('$CSVRecord[3]'), trim('$CSVRecord[4]'));";

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ewarmourAuthor Commented:
Thanks wasifig, that worked perfectly.
Muhammad WasifCommented:
Your are welcome

Wasif Ghani
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.