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 已经在数据库中,需要更新。另外两个将被插入

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

4

2 回答 2

2

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

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
于 2010-11-01T04:43:10.423 回答
1
  1. 使用move_uploaded_file将文件上传到目录
  2. 使用fgetcsv读取上传的 csv 并根据需要处理每一行。
  3. 删除 csv 文件
于 2010-11-01T05:10:03.713 回答