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 :)

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Edited by cyber_alchemist
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.