1

I have this table in csv file

enter image description here

Now i have two mysql tables dealing with this csv file,

Data A, B, C has to get stored in Table1 whereas data D, E, F, G, H has to get stored in Table2.

I have the above formatted csv file, how can i upload its data to MYSQL database??

So that from same file input can be done for different tables.

2

3 Answers 3

1

Try this it's working well, you can add as many values as possible depending on the number of columns you have in the CSV file.

  <?php   

 $fname = $_FILES['csv_file']['name'];     
  $chk_ext = explode(".",$fname);             

        $filename = $_FILES['csv_file']['tmp_      name'];   
 $handle = fopen($filename, "r");      
 if(!$handle){
 die ('Cannot open file for reading');
  }      
              while (($data = fgetcsv($handle,      10000, ",")) !== FALSE)
 {
          $query = "INSERT INTO tablename        (col1_csv, col2_csv)
 values ('$data[0]', '$data[1]');
   mysql_query($query) 
   or die(mysql_error());


        $query1 = "INSERT INTO tablename        (col1_csv, col2_csv)
    values ('$data[0]', '$data[1]');
   mysql_query($query1) 
 or die(mysql_error());
 }
 fclose($handle);
  ?>
Sign up to request clarification or add additional context in comments.

Comments

1

Probably only via custom script (PHP or so)...

But it's usually not hard to split CSV into 2 files and import both into separate tables using LOAD_DATA function from MySQL directly. It will be MUCH faster than using phpMyAdmin or similar scripts.

Comments

0

Load the data in a temporary table, then split up in two tables.

Then drop that temporary table. I somewhere read that php can do this, but I cannot help in php codes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.