php - 使用 PHP 插入数据库 - 仅插入一行

标签 php mysql foreach insert

我正在尝试将多行从文本文件插入到表中。 下面的代码似乎只插入文本文件中的第一行。我怎样才能改变它,以便它循环并插入其余的行?

$rwy = "./rwy.txt";

if (file_exists($rwy)) {
sql('TRUNCATE rwy_current');

$array = file($rwy);

foreach ($array as $line) {
    $bits = explode("\r\n", $line);
    foreach ($bits as $row) {
        $col = explode("\t", $row);
        sql("INSERT INTO rwy_current (arpt_ident, high_ident, low_ident, high_hdg, low_hdg, length, surface, pcn, he_elev, he_slope, he_tdze, le_elev, le_slope,
              le_tdze, he_true_hdg, le_true_hdg, heland_dis, he_takeoff, leland_dis, le_takeoff, cycle_date)
             VALUES ('$col[0]', '$col[1]', '$col[2]', '$col[3]', '$col[4]', '$col[5]', '$col[7]', '$col[8]', '$col[13]', '$col[14]', '$col[15]', '$col[30]',
              '$col[31]', '$col[32]', '$col[43]', '$col[44]', '$col[46]', '$col[47]', '$col[48]', '$col[49]', '$col[50]')
           ");
        }
    }
}

最佳答案

alter table rwy_current add primary key (arpt_ident, high_ident, low_ident);

解决了

问题出在数据库中;我只有一组主键。

关于php - 使用 PHP 插入数据库 - 仅插入一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40002783/

相关文章:

javascript - 对象数组 : Create new object for each specified key that has a value

c# - ForEach 不会更改/更改列表 C# 中的项目

PHP Web服务,在脚本执行结束前发送响应

javascript - 如何使用java脚本或jquery从php选择项获取值,其中选择项值来自查询mysql

php - 处理来自 php 和 jQuery 的 json 变量,改为给出文本变量

php - 使用 PHP 连接到 MySQL Fabric

php - 扩展数据、减少加载时间、让我的虚拟主机满意的最佳方式

mysql - 如果 current_user 将帖子标记为 'hidden',则独立隐藏帖子

c++ - delete-keyword 在这个 for-each 循环中是如何工作的?

JavaScript 从数据库中检索数据