I am writing a script to import CSV files into existing tables within my database. I decided to do the insert/update operations myself using PHP and INSERT/UPDATE statements, and not use MySQL's LOAD INFILE command, I have good reasons for this.
What I would like to do is emulate the insert/update operations and display the results to the user, and then give them the option of confirming that this is OK, and then committing the changes to the d开发者_StackOverflowatabase.
I'm using InnoDB database engine with support for transactions. Not sure if this helps but was thinking down the line of insert/update, query data, display to user, then either commit or rollback transaction?
Any advise would be appreciated.
The easier method would be something like that.
On the preview page :
- buffer your php output
- start a transaction
- do your inserts and updates
- select your data
- output it however your want
- rollback
Then on the confirmation page :
- buffer output
- start a transaction
- do inserts and updates
- commit
- output what you want
You could import the data into a separate "import" table. Show the user the result form the table. If he clicks "OK", copy that data to the main table and drop the import table. If he clicks "Cancel", just drop the import table.
The import table name should be chosen on a by-user basis so different users don't overwrite their tables.
Transactions won't help you here because the transaction will roll back when the PHP import script is finished.
精彩评论