I am using the following script to import data into my mysql database from CSV files. The CSV is setup like this :
Name  Postcode
fred  hd435hg
bob   dh345fj
Above is what it looks like in excel, in raw csv format viewed in notepad it looks like this :
name,postcode
frank,ng435tj
The problem I am having is for some reason the postcode column isnt getting imported at all, also the header row is getting imported as a record too, is it possible to make it skip the first row ?. I have been through the code and cant see why the postcode is not being pulled in, it is very odd.
        <?php
    //database connect info here
    //check for file upload
    if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){
        //upload directory
        $upload_dir = "./csv";
        //create file name
        $file_path = $upload_dir . $_FILES['csv_file']['name'];
        //move uploaded file to upload dir
        if (!move_uploaded_file($_FILES['csv_file']['tmp_name'], $file_path)) {
            //error moving upload file
            echo "Error moving file upload";
        }
        //open the csv file for reading
        $handle = fopen($file_path, 'r');
        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
            //Access field data in $data array ex.
            $name = $data[0];
            $postcode = $data[1];
            //Use data to insert into db
            $sql = sprintf("INSERT INTO test (name, postcode) VALUES ('%s',%d)",
                        mysql_real_escape_string($name),
                        $postcode
                        );
            mysql_query($sql) or (mysql_query("ROLLBACK") and die(mysql_error() . " - $sql"));
        }
        //delete csv file
        unlink($file_path);
    }
    ?>
 
     
    