php - 使用 php 将 csv 导入 mysql 并

标签 php mysql csv import

嗨,我想通过 php 将我的folder.csv导入到mysql。但我需要

"Check box 'ON' for Replace table data with file"

,但我不知道怎么做。 这个请求sql有效,但我想将他转换为使用php导入并自动执行一些操作

 $commande="mysql -u root -p mydb LOAD DATA LOCAL INFILE 'D:/Programmes/wamp64/www/Importcsv/imports/test.csv' INTO TABLE myTable FIELDS TERMINATED BY ';' ";
    exec($commande);

所以事实上我需要感谢 php import my file.csv 并且我真的需要“复选框'ON'以用文件替换表数据”。我的 id 是主键和 a.i 所以我需要有这个选项来修改每个 id 中的数据。

祝你有美好的一天

最佳答案

我建议您对特定文件进行临时上传,这是上传的 HTML 表单:

<table width="600">
    <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
        <tr>
            <td width="20%">Select file</td>
            <td width="80%"><input type="file" name="file" id="file" /></td>
        </tr>
        <tr>
            <td>Submit</td>
            <td><input type="submit" name="submit" /></td>
        </tr>
    </form>
</table>

用户$_FILES["file"]["tmp_name"]获取该文件并像这样解析它:

上面的行将获取您的文件,您必须像这样解析它(我将为您创建一个函数):

function parseCSV($_FILES["file"]["tmp_name"]){
    $csv = [];
    $lines = file($file, FILE_IGNORE_NEW_LINES); //this will ignore new lines, 
    //meaning that it will jump to the next row at a new line
    foreach ($lines as $key => $value) {//go trough each line with the foreach loop
        $csv[$key] = str_getcsv($value, ';');//this will delimit each column of a row by 
    //';'(semicolon) it might aswell be whatever your need it to be
    }
    //use the block below to see if the CSV was parsed within an array
    //echo '<pre>';
    //print_r($csv);
    //echo '</pre>';
    //die;
     return $csv;
}

解析文件后(在数组中查看它),您可以查看有关如何在表中插入值的 W33 教程 w3schools tutorial

希望这对您有帮助! 祝你有美好的一天!

关于php - 使用 php 将 csv 导入 mysql 并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55181495/

相关文章:

php - 在 symfony 3.4 中将通知转换为异常

PHP - 默认情况下检查数组元素是否有键或者它的键是自动的

mysql - 如何查询一行

javascript - 加载脚本时有时会收到解析错误

php - 更改 phpunit.xml 中的 php.ini 配置选项

php - MySQL在数据范围内计数周期

Linux - 加入 2 个 CSV 文件

php - 将 csv 下载为 zip 格式

python - 使用 Pandas reshape 2 列的数据

php - for 循环与 isset() != null - 为什么?