dragonmeng2002 2017-11-01 19:55
浏览 52
已采纳

将csv的值存储在表数据库的不同列中

I need to do a form that stores values of a csv in a table database, the form I just did works but I need to store the values in a specific order. This is the csv im working with csv

I need to store the values in color in different colums of a table database like this csv

But when I run the form the values store like this

example

this is my form

 <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;
$energies = [];
while (($myData = fgetcsv($handle,1000,',')) !== FALSE) {
  $k++;
  if ( $k > 1 ) {
        $energies[] = $myData[3];
     }
}


list($e1, $e2, $e3) = $energies;
$query = "INSERT INTO metlab.table (energy1, energy2, energy3) VALUES ($e1, $e2, $e3)";

$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>
</body>

I cant figure out how to do it :/ Thanks in advanced

  • 写回答

2条回答 默认 最新

  • dougu2036 2017-11-01 20:12
    关注
        ..............
    
        $handle = fopen($fileTmpName, 'r');
        $k = 0;
        $energies = [];
        while (($myData = fgetcsv($handle,1000,',')) !== FALSE) {
          $k++;
          if ( $k > 1 ) {
                $energies[] = $myData[3];
             }
        }
    
        // 1 method
        // $values = join(',', $energies);
        // $query = "INSERT INTO metlab.table (energy1, energy2, energy3) VALUES ($values)";
    
        // 2 method
        list($e1, $e2, $e3) = $energies;
        $query = "INSERT INTO metlab.table (energy1, energy2, energy3) VALUES ($e1, $e2, $e3)";
    
        $run = mysql_query($query);
    
        ..............
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮