Yii防止在刷新时从上传的文件中重新插入数据到数据库

I have an upload form, where I upload csv files and then insert all rows to the database. If I hit refresh, all content is inserted again and again into the database. What is the best prectice to prevent this?

Thanks a lot! BR

The best way to prevent this behavior is to use a redirect to show the landing page. When a redirect is made the browser history records another url after the post action. in Yii you can achieve this by doing the following from the controller.

 $this->redirect("../path/to/action");

You can add a hidden input element to the form which holds a unique ID:

<input type="hidden" name="random" value="4b3403665fea6">

If you store this value in the database you can make sure that no CSV file will be uploaded with the same unique ID (after refresh).

A unique ID can be generated with a built-in PHP function uniqid()