“Unggah file CSV di PHP” Kode Jawaban

Impor data dari CSV ke DB PHP

<?php
$query = <<<eof
    LOAD DATA INFILE '$fileName'
     INTO TABLE tableName
     FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
     LINES TERMINATED BY '\n'
     IGNORE 1 LINES
    (field1,field2,field3,etc)
eof;

$db->query($query);
?>
Filthy Fly

Unggah file CSV di PHP

if (isset($_POST['import'])) {
    if ($_FILES['file']['name']) {
        $filename = explode(".", $_FILES['file']['name']);
        if ($filename[1] == 'csv') {
            $handle = fopen($_FILES['file']['tmp_name'], "r");
           $i = 0;
            while ($getData = fgetcsv($handle)) {
                if($i > 0){
                    $productName   = $getData[0];
                    $productDesc   = $getData[1];
                    $productQty   = $getData[2];
                    $productPrice   = $getData[3];
                    $productTotal   = $getData[4];
                    $sql = "INSERT INTO import (productName,productDesc,productQty,productPrice,productTotal) VALUES('" . $getData[0] . "','" . $getData[1] . "','" . $getData[2] . "','" . $getData[3] . "','" . $getData[4] . "')";
                    $result = mysqli_query($conn, $sql);
                    header("Location:import.php");
                }
                $i++;
            }
            fclose($handle);
        }
    }
}

Check your table name,column name & where you want to redirect using header function.
Tense Tapir

Jawaban yang mirip dengan “Unggah file CSV di PHP”

Pertanyaan yang mirip dengan “Unggah file CSV di PHP”

Lebih banyak jawaban terkait untuk “Unggah file CSV di PHP” di PHP

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya