Mysql 如何在SQL中追加而不是替换插入值

Mysql 如何在SQL中追加而不是替换插入值,mysql,sql,Mysql,Sql,我有一个保存数据的文本文件。它每行包含3列数据,每列之间用逗号分隔。我正在尝试从此文件创建数据库,但仅保存最后一行 示例文件: Something,More,7 Another,Thing,9 One,Extra,3 脚本: <?php $myFile = file('data.txt'); foreach ($myFile as $row){ list($a,$b,$c) = explode(',', $row); $insertStmt =

我有一个保存数据的文本文件。它每行包含3列数据,每列之间用逗号分隔。我正在尝试从此文件创建数据库,但仅保存最后一行

示例文件:

Something,More,7
Another,Thing,9
One,Extra,3
脚本:

<?php
$myFile = file('data.txt');

    foreach ($myFile as $row){
        list($a,$b,$c) = explode(',', $row);
        $insertStmt = "INSERT INTO `MYTABLE` (`id`, `a`, `b`, `c`)" . PHP_EOL
                    . "  VALUES (NULL, '$a', '$b', $c);";
    }
?>

脚本在文件中循环,并重新定义变量
$insertStmt
每个循环。这会适得其反,您需要将其附加到每个循环中。像这样的东西会更好用

<?php
$myFile     = file('data.txt');
$insertStmt = "";
$i          = 0;


    foreach ($myFile as $row) {
        list($a,$b,$c) = explode(',', $row);

        if( $i == 0 )
            $insertStmt = $insertStmt . "INSERT INTO `MYTABLE` (`id`, `a`, `b`, `c`) VALUES ";
        else 
            $insertStmt = $insertStmt . "(NULL, '$a', '$b', $c),";

        $i++;
    }

        $insertStmt = rtrim($insertStmt, ",");
        $insertStmt = $insertStmt . ";"
?>