Hardik
Hardik

Reputation: 1411

Refreshing browser add duplicate entry in database - php

I have a form in which user input some details and then click a save button to save the data on database. Now after click save if I refresh the page browser ask for resend. If we resend it then duplicate entry available in database. My database insert process is on same page. Can it possible to prevent resend after completing process on single page?

Thank You in advance

Upvotes: 1

Views: 1707

Answers (3)

paulsm4
paulsm4

Reputation: 121809

One way is to invalidate the browser's cache by adding something like this to your PHP code:

 header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" );
 header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
 header( "Cache-Control: no-cache, must-revalidate" );
 header( "Pragma: no-cache" );

Upvotes: 1

k102
k102

Reputation: 8079

  1. you can redirect users after submitting and performing insert using header("Location: somepage.php")

  2. you can (an i think should) use unique constraint in the db (e.g. for Postgres: CONSTRAINT c_name UNIQUE (field_name))

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799230

No. Add a hidden field to the form that uniquely identifies the transaction and refuse the update if you see the same one.

That, or preform a redirect to another page in order to turn it into a GET request.

Upvotes: 0

Related Questions