0

I have trouble storing some values of a csv in MySQL. I have a form that uploads a csv to a database and it works but I would like to store the values I need in different columns. values on the csv are positioned like this energy value1 value2 value3 I would like to store those values in different colums like this id-value1-value2-value3. But I can only store them In a single colum. This is my current code. Image of csv

<div class="container">
<?php
if(isset($_POST['uploadBtn'])){
    $fileName=$_FILES['myFile']['name'];
    $fileTmpName=$_FILES['myFile']['tmp_name'];
   
    $fileExtension=pathinfo($fileName,PATHINFO_EXTENSION);

    $allowedType = array('csv');
    if(!in_array($fileExtension,$allowedType)){?>

        <div class="alert alert-danger">
            INVALID FILE
        </div>
    <?php }else{

        $handle = fopen($fileTmpName, 'r');
        $k = 0;
        while (($myData = fgetcsv($handle,1000,','))!== FALSE){
         $k++;
          if ( $k > 1 ) {

               
                $energy         = $myData[3];
                 $energy2       = $myData[3];
                  $energy3      = $myData[3];
                
                
                

                $query = "INSERT INTO table (energy,energy_2,energy_3)
                VALUES ('".$energy."','".$energy2."','".$energy3."')";
                $run = mysql_query($query);
             }

        }
        if(!$run){
            die("error in uploading file".mysql_error());
        }else{ ?>
                <div class="alert alert-success">
                    SUCCESS
                </div>
    <?php   }
    }
}
    ?>

<form action="" method="post" enctype="multipart/form-data">
    <h3 class="text-center">
        RESULTS
    </h3></hr>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="file" name="myFile" class="form-control">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="submit" name ="uploadBtn" class="btn btn-info">
            </div>
        </div>
    </div>
</form>
</div>
Wenfang Du
  • 8,804
  • 9
  • 59
  • 90
Jiga
  • 13
  • 8
  • https://stackoverflow.com/questions/22630570/insert-csv-file-data-into-mysql – splash58 Nov 01 '17 at 17:31
  • What do you mean by "But I can only store them In a single colum"? Your query is already inserting values into three columns. If you mean that you are inserting the same value in all three columns, that's because you're assigning `$energy`, `$energy2`, and `$energy3` all to `$myData[3];`. Also, include (at least part of, if not all of) your CSV directly in the question. Linking to an image is not particularly helpful. – Patrick Q Nov 01 '17 at 17:32
  • I mean that for example I cant store all three values in one colum of my table, I want them to store one value-one colum, did you get it? – Jiga Nov 01 '17 at 19:07
  • The form actually works but for example if the values of the csv are: 1 2 3 When I insert those values on my db the insert work like this: values_field= value 1,value2,value3 I want it this way: value1_colum=value1 of csv value2_colum=value2 of csv value3_colum=value3 of csv – Jiga Nov 01 '17 at 19:09

0 Answers0