PHP keeps Inserting on refreshing

本小妞迷上赌 提交于 2020-01-06 19:55:09

问题


I am currently programming a website for a school project in which I have to organize daily schedules/reports.

Following Situation: I am inserting the following into PHP:

$sqlinserttb = "INSERT INTO tagesberichte (`TbID`, `Tagesberichte`, `Datum`) VALUES ('DEFAULT', '".$tagesbericht."', '".$datum."');";

If submit has been hit, it inserts it. The problem is when I press F5 or manually refresh the browser, the page inserts it again with the last used inputs.

What did I miss?


回答1:


Use header("location: <path to your self-submitting form>"); after you are done inserting, that way a fresh form will be displayed and refreshing should not lead to submitting again.




回答2:


From the use case you explained, I understand that you have a self submitting form (submitting to the same page and handling request). You could store your request variables in php sessions and then check the incoming request data with that in the session (if session exists). If same, then display an error message, else do the insert and then set the session with your request data.




回答3:


If your refresh the URL, all corresponding data will be sent to php script as it were posted by the user itself. To deal with it, you could: 1. Use post method to send result to the server. It will not exclude the situations with refresh of the page, but most browsers will ask the user "do you really want to resend all data to the server?" 2. Use redirect after the php script proceed the page with your input. In this case browser will not have the data to resend. But, the user could back to previos page still and resend the data again. 3. Also you could provide some kind of 'meta http-equiv="Cache-Control" ... ' variables to make page expired



来源:https://stackoverflow.com/questions/42031148/php-keeps-inserting-on-refreshing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!