Importing Multiple CSV Files to MySQL Tables

Import Multiple Csv file into mysql table

Html:

<input type="file" name="file[]" id="file" multiple>

php :

if (isset($_POST["submit"])) {
// Count total files
$countfiles = count($_FILES['file']['name']);

// Looping all files
for ($i = 0; $i < $countfiles; $i++) {
if ($_FILES['file']['name'][$i]) {
$filenametmp = $_FILES['file']['name'][$i];
$filename = explode(".", $filenametmp);

if ($filename[1] == 'csv') {
$handle = fopen($_FILES['file']['tmp_name'][$i], "r")
$counter = 0;
while (!feof($handle)) {
if ($counter === 2)
break;
$buffer = fgetcsv($handle, 5000);
++$counter;
}
while ($data = fgetcsv($handle)) {
if ($data[42] == "SignInName") {
$item0 = mysqli_real_escape_string($connect, $data[42]);
}
$item0 = mysqli_real_escape_string($connect, $data[42]);
$item1 = mysqli_real_escape_string($connect, $data[32]);
$item2 = mysqli_real_escape_string($connect, $data[13]);
$item3 = mysqli_real_escape_string($connect, $data[19]);
$item4 = mysqli_real_escape_string($connect, $data[44]);
$item5 = mysqli_real_escape_string($connect, $data[5]);
$query = "INSERT into csv(email, password, firstname,lastname,field,country) values('$item0','$item1','$item2','$item3','$item4','$item5')";
mysqli_query($connect, $query);
}
fclose($handle);
echo "<script>alert('uploaded');</script>";
} else {
echo "<script>alert('ERROR ')</script>";
}
}
}
}

Import multiple .csv files to mysql using bash

Looking at the od output of your CSV header, appears that you have a carriage return and line feed at the end of your CSV lines (the \r\n sequence). Did the CSV come from processing under Windows? \r\n is a common text line termination under Windows, whereas Linux uses only \n. The \r gets included as part of the string and is not considered part of the line termination. It also can yield strange looking output since the \r by itself, when output, resets the cursor position to the first column and subsequent characters display over the top of whatever may have been displayed on that same line previously.

I recommend running dos2unix on your CSV file first, then do your processing. I think that will clear up the odd error and help the processing of the rest of the file work properly as well.

Importing multiple csv files to mysql tables

why didn't the PDO object spew errors back at me for this?

Because MySQL executed your query without any errors. Only because you've written the wrong query this must not mean that the query is that wrong that MySQL won't accept it.

Whenever you generate SQL queries programmatically, verify (by debugging or even better unit-tests), that the query has been created right for what you want to do.

If you want to get an exception each time an error occurs, enable that:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Import multiple CSV files into mysql

You can link MYSQL directly to it and upload the information using the following SQL syntax.

load data local infile 'uniq.csv' into table tblUniq
fields terminated by ','
enclosed by '"'
lines terminated by '\n'

Read more here : LOAD DATA INFILE

Import CSV file directly into MySQL



Related Topics



Leave a reply



Submit