Jump to content

Import a CSV file into a specific column within a table


scuttzz

Recommended Posts

Hi all,

 

I have a little bit of a problem that I've been trying to solve for days now. Wonder if anyone could help.

I have a table(named 'products') within this table the headers are as follows id, product_name,price, long_desc, short_desc, date_added, product_spec, directions, nut_info.

I have a CSV file with colums ranging from 2 columns to 4, so a total of 3 tables.. Is there a way to import them to only the nut_info column in my Mysql table?

Thanks in advance :)

Coincidences :P , i have to do the same thing and was figuring out how ...

csv file format is i guess like this ...

Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38 

where every value is separated by a comma , so i guess on could use explode(); to takeout values from it :)

 

http://php.net/manual/en/function.explode.php

 

it would help if one could get a format of the file and a example data inserted into it :)

The format is also separated by comma's, the first table has 3 headings (Typical nutritional values, per 100g, per 20g serving ), and the information within it is numbers and a few letters.. How ever for this specific row theres a total of 3 tables, but im sure that when i've figured out out to import the one the rest are simple. Thanks for the article ill give it a read... And to do with the format, its was originally a excel(.xls) file that I converted into a CSV through excel 2007. Does that answer all your questions?

looked into the .csv files with comma , commas are escaped by 

"

such as

,"

any conditional statement will solve it with explode :) isnt it ??? anyways

 

here is the short script with the help of the example of php.net:

 

excelupload.php

<?php
if (isset($_POST['submit'])) {
   $excel_name = $_FILES['excel']['name'];
   echo $excel_name . "<br />";
   $excel_tmp_name = $_FILES['excel'][ 'tmp_name'];
   echo $excel_tmp_name . "<br />";
   $excel_file_size = $_FILES['excel']['size'];
   echo $excel_file_size . "<br />";
   $excel_file_type = $_FILES['excel']['type'];
   echo $excel_file_type . "<br />";
   $excel_date = @date('y-m-d');
   echo 'Date of file upload' . $excel_date;

   $row = 1;
   echo '<table border="1" >';
   if (($handle = fopen($excel_tmp_name, "r")) !== FALSE) {
       while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
          $num = count($data);
          echo "<tr> <td>$num</td><td>$row:</td>";
          $row++;
          for ($c=0; $c < $num; $c++) {
              echo "<td>" . $data[$c] . "</td>\n";
          }
          echo "</tr>";
       }
      fclose($handle);
   }
   echo "</table>";
}
?>

excel.html

<html>
<head><title>excel upload</title></head>
<body>
<form action="excelupload.php" method="post" enctype="multipart/form-data" >
<input type="file" name="excel" id="excel" />
<input type="submit" name="submit" id="submit" value="submit" />
</form>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.