Jump to content

CSV encoding - black diamonds!


tomhoad

Recommended Posts

I'm importing a CSV using the fgetcsv() function, which is working all good.

 

However, when I take a look at the data in the database, I see black diamonds with question marks. This isn't too much of an issue when echoing the data back out again, as they don't appear, but when I want to use one of the CSV fields as a MySQL date, it isn't recognised as a date and is stored as 0000-00-00.

 

e.g.

 

gEvqf.png

 

I think this issue is something to do with encoding of the CSV? Can anyone offer any advice?

 

If it helps here is my import script, and the encode type is ASCII according to mb_detect_encoding

 

<?php
include 'config.php';
include 'opendb.php';
ini_set("auto_detect_line_endings", true);

$row = 0;
$tmpName = $_FILES['csv']['tmp_name'];

if (($handle = fopen($tmpName, "r")) !== FALSE) {
    $num = count($data);

     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
     {
        $noQuotes = str_replace("\"", '', $data);
        $originalDate = $noQuotes[1];

        //$delivery_date = date('Y-m-d', strtotime($originalDate));

        $parts = explode('/', $originalDate);
        $delivery_date = $parts[2] . '-' . $parts[1] . '-' . $parts[0];

        $row++;

        $import="INSERT into dispatch (delivery_note_number, delivery_date, dispatch_date, customer_delivery_date, delivery_line, produce, variety, quantity, pallets, count, depot, customer, grower, haulier, status) 
        values ('$noQuotes[0]', '$delivery_date', '$noQuotes[2]', '$noQuotes[3]', '$noQuotes[4]', '$noQuotes[5]', '$noQuotes[6]', '$noQuotes[7]', '$noQuotes[8]', '$noQuotes[9]', '$noQuotes[10]', '$noQuotes[11]', '$noQuotes[12]', '$noQuotes[13]', '$noQuotes[14]')";

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

        //header("location:list_dispatch.php?st=recordsadded");


    fclose($handle);
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.