Forum Moderators: coopster

Message Too Old, No Replies

Importing a CSV into MySQL

         

Mark1978

11:12 am on May 17, 2010 (gmt 0)

10+ Year Member



Hi, I am new to this place, so go easy on me.

I have reated a PHP script that allows a user to download there contacts that are stored in a MySQL table into a CSV. They need to ammend the contacts and then re-upload the CSV back into the database via PHP.

What I want to know instead of uploading the updated CSV back into table, can the table be overwritten?

Thanks

jatar_k

1:15 pm on May 17, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Mark1978,

I guess you could do that, though you would delete all the contents and then insert what they upload.

You would want to be careful with doing that as if the upload failed you could end up with no data.

rocknbil

5:17 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On upload, you'd do

delete from table;

then insert the new records. But I wouldn't (and haven't) done it that way.

You have two tables. One for live data, one for uploaded data. If you have multiple users, there will be an extra field in the uploaded data field so the user's data doesn't get mixed in with everyone else's.

On upload, you parse out the CSV data, insert it into the uploaded table, on success, you force them through checking it. This is one of the few cases where you should really force them, people are lazy and just allowing them to commit to the live table can be disastrous.

After a review of the data, a commit button will delete the data from the live table, then insert the new data from the upload table. When done, be sure to clean up,

delete from uploaded where user_id=(this user's id)

It may get a little more complicated if you have multiple users contributing to the live contacts table, but this is a start.