如何使用列名而不是索引号的PHP将CSV / excel文件上传到MySql


How to upload CSV/excel file to MySql using PHP with column name not index number?

我的代码运行良好。但是有一个小问题,比如我的csv文件列号随着时间的推移而不断变化。而且不可能每次都更改代码中的索引号。所以我找到了一个解决方案,就像我可以通过列名而不是数字读取 csv。 如何根据列号的列名获取csv文件的记录?提前谢谢。

 if ($_POST["upload"] == "freight") {
        $file = $_FILES['file']['tmp_name'];
        $handle = fopen($file, "r");
        $c = 0;
        $branch=$_POST["freight"];
        while (($filesop = fgetcsv($handle, 3000000, ",")) !== false) {
            $invoice_number = $filesop[3];
            $freaight3 = $filesop[12];
            $freaight2 = substr($freaight3, 0, -2);
            if($freaight2=="")
            {
                $freaight2=0;
            }
            $with_tax = $filesop[10];
            $with_tax2 = substr($with_tax, 0, -2);
            $Invoice_Number = $branch."_" .$invoice_number;
            $update_freaight = "UPDATE xyz SET `Total_Freight`=" . $freaight2 . ",`Value_With_Tax`='" . $with_tax2 . "' where `Invoice_Number`='" . $Invoice_Number . "'";
          //   echo $update_freaight;
            $update = mysqli_query($con, $update_freaight);
        }
        if ($update) {
            $massage = "You database has imported successfully. You have inserted " . $c . " recoreds";
        } else {
            $massage = "Sorry! There is some problem.";
        }
    }
最后,

通过 11 小时的搜索,我自己做了。不是正确的方法,但是是的,它在逻辑上是正确的!

   $file = $_FILES['file']['tmp_name'];
            $csv = array_map("str_getcsv", file("$file",FILE_SKIP_EMPTY_LINES));
            $keys = array_shift($csv);
            for($i=0;$i<=sizeof($keys);$i++)
            {
                $name=$keys[$i];
                if($name=="xyz") {
                    $xyz = $i;
                    continue;
                }
               else if($name=="abc") {
                    $abc = $i;
                    continue;
                }
              else if($name=="tax") {
                    $tax = $i;
                    continue;
                }
}
if ($_POST["upload"] == "freight") {
        $file = $_FILES['file']['tmp_name'];
        $handle = fopen($file, "r");
        $c = 0;
        $branch=$_POST["freight"];
        while (($filesop = fgetcsv($handle, 3000000, ",")) !== false) {
            $xyz = $filesop[$xyz];
            $abc = $filesop[$abc];
            $freaight2 = substr($abc, 0, -2);
            if($freaight2=="")
            {
                $freaight2=0;
            }
            $with_tax = $filesop[$tax];
            $with_tax2 = substr($with_tax, 0, -2);
            $Invoice_Number = $branch."_" .$invoice_number;
            $update_freaight = "UPDATE xyz SET `Total_Freight`=" . $freaight2 . ",`Value_With_Tax`='" . $with_tax2 . "' where `Invoice_Number`='" . $Invoice_Number . "'";
          //   echo $update_freaight;
            $update = mysqli_query($con, $update_freaight);
        }
        if ($update) {
            $massage = "You database has imported successfully. You have inserted " . $c . " recoreds";
        } else {
            $massage = "Sorry! There is some problem.";
        }
    }