PHP keeps Inserting on refreshing

2019-09-08 06:22发布

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?

3条回答
爷、活的狠高调
2楼-- · 2019-09-08 06:29

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楼-- · 2019-09-08 06:36

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

查看更多
▲ chillily
4楼-- · 2019-09-08 06:44

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.

查看更多
登录 后发表回答