簡體   English   中英

僅從 csv 導入 php 中的 mysql 表中的一行

[英]only one row importing from csv to mysql table in php

我正在嘗試將 CSV 文件中的記錄導入 MySQL 表,但只插入第一行。 剩余未上傳到數據庫

if (isset($_POST["upload_csv"])) {

    $fileName = $_FILES["file"]["tmp_name"];

    if ($_FILES["file"]["size"] > 0) {

        $file = fopen($fileName, "r");

        while (($column = fgetcsv($file, 10000, ",")) !== FALSE) {
            $sqlInsert = "INSERT into price_dump (`stock_id`,`stock_price`,`previous_price`,`minute_set`)
                   values ('" . $column[0] . "','" . $column[1] . "','" . $column[2] . "','" . $column[3] . "')";
            $result = mysqli_query($conn, $sqlInsert);

            if (! empty($result)) {
                $type = "success";
                $message = "CSV Data Imported into the Database";
            } else {
                $type = "error";
                $message = "Problem in Importing CSV Data";
            }
        }
    }
}
if (isset($_POST["upload_csv"])) {

    $fileName = $_FILES["file"]["tmp_name"];

    if ($_FILES["file"]["size"] > 0) {

        $file = fopen($fileName, "r");
        $lines = file($_FILES["file"]["tmp_name"]);

        foreach ($lines as $line) {
            $column = explode(',', $line);
            $sqlInsert = "INSERT into price_dump (`stock_id`,`stock_price`,`previous_price`,`minute_set`)
                   values ('" . $column[0] . "','" . $column[1] . "','" . $column[2] . "','" . $column[3] . "')";
            $result = mysqli_query($conn, $sqlInsert);

            if (! empty($result)) {
                $type = "success";
                $message = "CSV Data Imported into the Database";
            } else {
                $type = "error";
                $message = "Problem in Importing CSV Data";
            }
        }
    }
}

也許 csv 文件被分隔;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM