php - 将CSV文件导入mysql数据库

标签 php mysql csv

我正在使用 php 将 csv 文件导入数据库表。当我选择文件并提交时,我插入了行,但只有表 ID 和递增的文件编号,其他列为空

以下是php代码:

<html>
<head></head>       
    <form name="import" method="post" enctype="multipart/form-data">
        <input type="file" name="file" /><br />
        <input type="submit" name="submit" value="Submit" />
    </form>
<?php       
    if(isset($_POST["submit"]))
    {
        $file = $_FILES['file']['tmp_name'];
        $handle = fopen($file, "r");
        $c = 0;

    $db = new PDO('mysql:host=localhost;dbname=apdatabase', 'root', '');
    $db->exec("SET CHARACTER SET utf8");

        while(($filesop = fgetcsv($handle, 1000,";")) !== false)
        {               
             $filenumb          = $filesop['filenumb']; 
    $hospitalname          = $filesop['hospitalname']; 
    $department       = $filesop['department']; 
    $unit      = $filesop['unit']; 
    $operdt = $filesop['operdt'];
    $reason_desc       = $filesop['reason_desc']; 
    $notes      = $filesop['notes']; 
    $full_name       = $filesop['full_name']; 
    $dataentrytime      = $filesop['dataentrytime']; 

    $sql = "INSERT INTO can_surg (can_surg_id, filenumb, hospitalname, department, unit, operdt, reason_desc, notes, full_name, dataentrytime) VALUES (NULL, $filenumb,'$hospitalname','$department','$unit','$operdt','$reason_desc','$notes','$full_name', '$dataentrytime')";
            $c = $c + 1;
            $result = $db->query($sql);
        }
        if($sql){
            echo "You database has imported successfully. You have inserted ". $c ." recoreds";
        }else{
            echo "Sorry! There is some problem.";                   
        }    
    }
?>
</html>

最佳答案

编辑代码;

$db = new PDO('mysql:host=localhost;dbname=apdatabase', 'root', '');
$db->exec("SET CHARACTER SET utf8");
if(isset($_POST["submit"])) {
    $file = $_FILES["file"]["tmp_name"];
    if($_FILES["file"]["size"] > 0){
    $file = fopen($filename, "r");
        while(($filesop = fgetcsv($file, 10000, ",")) !== FALSE){ 
            // You have to set values according to columns, e.g , filenumb is 0 column in your csv file
            $filenumb  = '$filesop[0]'; 
            $hospitalname  = '$filesop[1]'; 
            $department = '$filesop[2]'; 
            $unit = '$filesop[3]'; 
            $operdt = '$filesop[4]';
            $reason_desc = '$filesop[5]'; 
            $notes = '$filesop[6]'; 
            $full_name = '$filesop[7]'; 
            $dataentrytime = '$filesop[8]'; 

            $sql = "INSERT INTO can_surg (can_surg_id, filenumb, hospitalname, department, unit, operdt, reason_desc, notes, full_name, dataentrytime) VALUES (NULL, $filenumb,'$hospitalname','$department','$unit','$operdt','$reason_desc','$notes','$full_name', '$dataentrytime')";
            $result = $db->query($sql);
        }
    }
    fclose($file);
}

现在尝试上面的代码

关于php - 将CSV文件导入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31361983/

相关文章:

javascript - 如何在 php 循环函数上应用 html/css

mysql - 通过sql查询复制行

pandas - 读取文件夹中的多个 Parquet 文件,然后使用python写入单个csv文件

bash - PostgreSQL 从远程服务器将结果导出为 CSV

python - 在 python 中编辑 CSV 文件,该文件从 python 中的另一个 json 文件读取值

php - 从电话号码中删除特殊字符

php - 使用php从mysql打印数据

mysql - 通过引用另一个表中的记录对记录进行排序 Rails 4

mysql - 如何规范我当前的数据库结构

javascript - 解析复杂的 HTML 表格