I have tried importing csv file in database but the problem is it inserts a blank record in the 1st row of database and also the date is not gettin stored in yyyy-mm-dd format also the time is not diplayed in database the datatyse is set one heto date time format in excel sheet the date is in dd-mm-yyyy format ....can any one help me to handle the date isue in the following code and also to avoid the 1st balnk record in database....thanks in advance

<?php

include "connection.php"; //Connect to Database

$deleterecords = "TRUNCATE TABLE biological_zb"; //empty the table of its current records
mysql_query($deleterecords);

//Upload File
if (isset($_POST['submit'])) {
    if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
        echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
        echo "<h2>Displaying contents:</h2>";
        readfile($_FILES['filename']['tmp_name']);
    }



    //Import uploaded file to Database
    $handle = fopen($_FILES['filename']['tmp_name'], "r");


    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {


$import="INSERT into biological_zb(INODC_PR_N, INODC_ST_N, PROJ_NO,ORG_ST_NO,ST_DATE,LATITUDE,LONGITUDE,EEZ,DEPTH,SS,TRB,BOD,PHC,POC,PHEN) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]','$data[14]')";

        mysql_query($import) or die(mysql_error());

}



    fclose($handle);

    print "Import done";

    //view upload form
}else
 {

    print "Upload new csv by browsing to file and clicking on Upload<br />\n";

    print "<form enctype='multipart/form-data' action='import_excel.php' method='post'>";

    print "File name to import:<br />\n";

    print "<input size='50' type='file' name='filename'><br />\n";

    print "<input type='submit' name='submit' value='Upload'></form>";

}

?>



include "connection.php"; //Connect to Database

$deleterecords = "TRUNCATE TABLE biological_zb"; //empty the table of its current records
mysql_query($deleterecords);

//Upload File
if (isset($_POST['submit'])) {
    if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
        echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
        echo "<h2>Displaying contents:</h2>";
        readfile($_FILES['filename']['tmp_name']);
    }



    //Import uploaded file to Database
    $handle = fopen($_FILES['filename']['tmp_name'], "r");


    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {


$import="INSERT into biological_zb(INODC_PR_N, INODC_ST_N, PROJ_NO,ORG_ST_NO,ST_DATE,LATITUDE,LONGITUDE,EEZ,DEPTH,SS,TRB,BOD,PHC,POC,PHEN) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]','$data[14]')";

        mysql_query($import) or die(mysql_error());

}



    fclose($handle);

    print "Import done";

    //view upload form
}else
 {

    print "Upload new csv by browsing to file and clicking on Upload<br />\n";

    print "<form enctype='multipart/form-data' action='import_excel.php' method='post'>";

    print "File name to import:<br />\n";

    print "<input size='50' type='file' name='filename'><br />\n";

    print "<input type='submit' name='submit' value='Upload'></form>";

}

?>

date("Y-m-d H:i:s",strtotime($your_data_field))

$your_data_field is the data which you read from file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.