How to upload CSV/excel file to MySql using PHP with column name not index number? -
my code working good. there little issue csv file column numbers keeps changing time. , it's not possible change index number in code every time. have found solution can read csv column name not number. how can fetch record of csv file based on column name behalf of column number? in advance.
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 . "' `invoice_number`='" . $invoice_number . "'"; // echo $update_freaight; $update = mysqli_query($con, $update_freaight); } if ($update) { $massage = "you database has imported successfully. have inserted " . $c . " recoreds"; } else { $massage = "sorry! there problem."; } }
finally 11 hours search did own. not right way yeah logically right!.
$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 . "' `invoice_number`='" . $invoice_number . "'"; // echo $update_freaight; $update = mysqli_query($con, $update_freaight); } if ($update) { $massage = "you database has imported successfully. have inserted " . $c . " recoreds"; } else { $massage = "sorry! there problem."; } }
Comments
Post a Comment