kush
kush

Reputation: 93

Duplicacy Issue In PHP /MYsql

I am inserting a row in mysql table. The row is getting inserted successfully in the table. But the issues is that whenever I refresh the page, a new row is added in the database.

How to prevent it?

Upvotes: 3

Views: 221

Answers (3)

Sanks R
Sanks R

Reputation: 895

Use primary key in mysql table. Make any column as primary key column

Upvotes: 0

6nagi9
6nagi9

Reputation: 544

This is browser's issue. They repost data on refresh. To prevent it from happening, you'll have to check for duplication of data in your code each time a new record is inserted. This is the general practice where duplication of data has to be prevented.

In another case where a genuine duplicate data is expected and allowed, what you can do is to redirect user on to the same registration page after each insertion by header("Location: {$location}");. This will clear the POST data of the browser.

Upvotes: 1

alex
alex

Reputation: 490183

Use the Post/Redirect/Get pattern.

Post/Redirect/Get (PRG) is a common design pattern for web developers to help avoid certain duplicate form submissions and allow user agents to behave more intuitively with bookmarks and the refresh button.

In PHP, this looks like so, generally...

<?php

handleForm($_POST);

header('Location: next-page.php');
exit;

Obviously handleForm() is to be substituted for your code that handles the inserts etc.

Upvotes: 7

Related Questions