2010-11-01 74 views
0

好了,所以我有一個數據庫表叫請求這種結構使用PHP到MySQL和更新以及

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 |    | 
+------------+--------------+------+-----+---------+----------------+ 

這裏上傳CSV是一些示例數據

+----+-----------+-------------------------+------------+--------+ 
| 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這已經是在數據庫中,需要更新..其他兩個將被插入

我不是要求某人編寫整個腳本,但如果我可以得到一些方向的上傳,然後從那裏去.. ..感謝

回答

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 [email protected]) -- Note that dummy1 as my table. 

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