gpt4 book ai didi

php - 如何将插入旧值传递给 mysql?它也插入旧行

转载 作者:行者123 更新时间:2023-11-29 05:28:37 24 4
gpt4 key购买 nike

我有一个 txt 文件,它像 rss 一样每 5 分钟更新一次值。我用 curl 记录值,它会自动插入到 Mysql。

但是有个问题。在 txt 文件中,当我记录新行时,它会用旧行插入它们。

要插入的 Php 行。

$addvalue = "INSERT INTO city SET cities='$cities', date='$date'";
mysql_query($addvalue)

Mysql行

INSERT INTO `city` VALUES (1, 'ISTANBUL', '2013-06-19 18:09:39');
INSERT INTO `city` VALUES (2, 'IZMIR', '2013-06-19 15:06:35');
INSERT INTO `city` VALUES (3, 'LONDON', '2013-06-19 11:17:15');
INSERT INTO `city` VALUES (4, 'BERLIN', '2013-06-18 06:12:21');

有了新行,它再次保存旧行

INSERT INTO `city` VALUES (1, 'ISTANBUL', '2013-06-19 18:09:39');
INSERT INTO `city` VALUES (2, 'IZMIR', '2013-06-19 15:06:35');
INSERT INTO `city` VALUES (3, 'LONDON', '2013-06-19 11:17:15');
INSERT INTO `city` VALUES (4, 'BERLIN', '2013-06-18 06:12:21');
INSERT INTO `city` VALUES (1, 'ISTANBUL', '2013-06-19 18:09:39');
INSERT INTO `city` VALUES (2, 'IZMIR', '2013-06-19 15:06:35');
INSERT INTO `city` VALUES (3, 'LONDON', '2013-06-19 11:17:15');
INSERT INTO `city` VALUES (4, 'BERLIN', '2013-06-18 06:12:21');
INSERT INTO `city` VALUES (5, 'MOSCOW', '2013-06-20 19:15:33');
INSERT INTO `city` VALUES (6, 'PARIS', '2013-06-20 12:18:32');
INSERT INTO `city` VALUES (7, 'NEW YORK', '2013-06-20 21:17:25');
INSERT INTO `city` VALUES (8, 'OSLO', '2013-06-20 07:17:44');

我试着像那样保存它们

INSERT INTO `city` VALUES (1, 'ISTANBUL', '2013-06-19 18:09:39');
INSERT INTO `city` VALUES (2, 'IZMIR', '2013-06-19 15:06:35');
INSERT INTO `city` VALUES (3, 'LONDON', '2013-06-19 11:17:15');
INSERT INTO `city` VALUES (4, 'BERLIN', '2013-06-18 06:12:21');
INSERT INTO `city` VALUES (5, 'MOSCOW', '2013-06-20 19:15:33');
INSERT INTO `city` VALUES (6, 'PARIS', '2013-06-20 12:18:32');
INSERT INTO `city` VALUES (7, 'NEW YORK', '2013-06-20 21:17:25');
INSERT INTO `city` VALUES (8, 'OSLO', '2013-06-20 07:17:44');

我不想在 txt 中保存相同的旧行。当新行出现时,它应该添加到旧行的 mysql 底部。我该怎么做?

最佳答案

如果在要插入值时将城市名称作为唯一索引,则会显示错误。所以你会避免重复的条目。

来自 manual:

A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. This constraint does not apply to NULL values except for the BDB storage engine. For other engines, a UNIQUE index permits multiple NULL values for columns that can contain NULL. If you specify a prefix value for a column in a UNIQUE index, the column values must be unique within the prefix.

关于php - 如何将插入旧值传递给 mysql?它也插入旧行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17214685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com