使用 php 将 csv 上传到 MySQL 并更新

发布于 2024-09-29 15:52:59 字数 1721 浏览 0 评论 0原文

好的,我有一个名为 requests 的数据库表,其结构

mysql> desc requests;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| artist     | varchar(255) | YES  |     | NULL    |                |
| song       | varchar(255) | YES  |     | NULL    |                |
| showdate   | date         | YES  |     | NULL    |                |
| amount     | float        | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

如下,这是一些示例数据,

+----+-----------+-------------------------+------------+--------+
| id | artist    | song                    | showdate   | amount |
+----+-----------+-------------------------+------------+--------+
|  6 | Metallica | Hello Cruel World       | 2010-09-15 | 10.00  |
|  7 | someone   | Some some               | 2010-09-18 | 15.00  |
|  8 | another   | Some other song         | 2010-11-10 | 45.09  |
+----+-----------+-------------------------+------------+--------+

我需要一种方法,能够为用户提供上传具有相同结构的 csv 的方法,并根据 csv 中的内容更新或插入。我在网上找到了很多脚本,但大多数都有硬编码的 csv,这不是我需要的。我需要用户能够上传 csv...用 php 很容易吗...

这是一个示例 csv

id  artist          song           showdate    amount
11  NewBee          great stuff    2010-09-15  12.99
10  anotherNewbee   even better    2010-09-16  34.00
6    NewArtist       New song       2010-09-25  78.99

正如你所看到的,我的 id 6 已经在数据库中并且需要更新..另外两个将被插入,

我并不是要求某人编写整个脚本,但如果我可以在上传上获得一些指导,然后从那里去哪里......谢谢

Ok so i have a database table called requests with this structure

mysql> desc requests;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| artist     | varchar(255) | YES  |     | NULL    |                |
| song       | varchar(255) | YES  |     | NULL    |                |
| showdate   | date         | YES  |     | NULL    |                |
| amount     | float        | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

Here is some example data

+----+-----------+-------------------------+------------+--------+
| id | artist    | song                    | showdate   | amount |
+----+-----------+-------------------------+------------+--------+
|  6 | Metallica | Hello Cruel World       | 2010-09-15 | 10.00  |
|  7 | someone   | Some some               | 2010-09-18 | 15.00  |
|  8 | another   | Some other song         | 2010-11-10 | 45.09  |
+----+-----------+-------------------------+------------+--------+

I need a way to be able to give user a way to upload a csv with the same structure and it updates or inserts based on whats in the csv. I have found many scripts online but most have a hard coded csv which is not what i need. I need the user to be able to upload the csv...Is that easy with php....

Here is an example csv

id  artist          song           showdate    amount
11  NewBee          great stuff    2010-09-15  12.99
10  anotherNewbee   even better    2010-09-16  34.00
6    NewArtist       New song       2010-09-25  78.99

As you can see i have id 6 which is already in the database and needs to be updated..The other two will get inserted

I am not asking for someone to write the whole script but if i can get some direction on the upload and then where to go from there....thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘曦 2024-10-06 15:52:59

创建如下存储过程并测试它。这是作品

CREATE proc csv

(
@id int,
@artist varchar(50),
@songs varchar(100),
@showdate datetime,
@amount float
)
as
set nocount on

if exists (select id from dummy1 where id=@id) -- Note that dummy1 as my table.

begin
update dummy1 set artist= @artist where id=@id
update dummy1 set songs=@songs where id=@id
update dummy1 set showdate=@showdate where id=@id
update dummy1 set amount=@amount where id=@id
end
else
insert into dummy1  (artist,songs,showdate,amount)values(@artist,@songs,@showdate,@amount)
Go

Create store procedure as below and test it. It is works

CREATE proc csv

(
@id int,
@artist varchar(50),
@songs varchar(100),
@showdate datetime,
@amount float
)
as
set nocount on

if exists (select id from dummy1 where id=@id) -- Note that dummy1 as my table.

begin
update dummy1 set artist= @artist where id=@id
update dummy1 set songs=@songs where id=@id
update dummy1 set showdate=@showdate where id=@id
update dummy1 set amount=@amount where id=@id
end
else
insert into dummy1  (artist,songs,showdate,amount)values(@artist,@songs,@showdate,@amount)
Go
终难愈 2024-10-06 15:52:59
  1. 使用move_uploaded_file 将文件上传到目录使用
  2. < a href="http://php.net/manual/en/function.fgetcsv.php" rel="nofollow noreferrer">fgetcsv 读取上传的 csv 并根据需要处理每一行。
  3. 删除 csv 文件
  1. upload the file to a directory using move_uploaded_file
  2. use fgetcsv to read the uploaded csv and process each row as you like.
  3. delete the csv file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文