Hi..I have a problem on database insertion:(

How to prevent duplicate record insertion on database while refreshing a webpage?
I had tried the following code,but it also resubmitting the form data.

if(isset($_REQUEST['add_cat']))
{

  //code for db insertion

}

Thanks in advance

Dani AI

Generated

A few reliable, practical ways to stop a POST from inserting the same row multiple times — and why some of the ideas already in this thread are incomplete.

was right: the Post/Redirect/Get (PRG) pattern is the simplest fix for browser refresh resubmits. When redirecting is truly not an option (as noted), PRG can still be used while showing results by saving the data-to-display in a short-lived session "flash" value, redirecting, then reading-and-clearing that session value. is also correct to mention tokens/sessions. By contrast, the $NOMOREINSERTION approach from will not work because plain PHP variables do not persist across HTTP requests, and unset($_POST) only affects the current script — the browser will still re-send the same POST on refresh.

Always enforce uniqueness on the server and database side (this is the last, reliable line of defence). Example SQL to prevent duplicate usernames:

ALTER TABLE login
  ADD CONSTRAINT uniq_username UNIQUE (username);

Handle duplicate-key errors in application code (SQLSTATE 23000 / MySQL errno 1062) and show a friendly message instead of inserting again.

A robust token/idempotency pattern (server-side) prevents double processing even without redirect:

// on form render
session_start();
$token = bin2hex(random_bytes(16));
$_SESSION['form_token'] = $token;

Add a hidden input with that token. On POST, verify with hash_equals($_SESSION['form_token'], $_POST['form_token']); then unset($_SESSION['form_token']) immediately after a successful insert so a refresh cannot reuse it.

Extra notes: combine methods — DB unique constraints, server-side token checks, and a client-side UX guard (disable the submit button after first click) for best results. Also move away from deprecated mysql_* calls: use PDO or MySQLi with prepared statements to avoid SQL injection and to catch duplicate-key exceptions cleanly.

Recommended Answers

All 4 Replies

After processing the POST page(db insertions), redirect the user to the same page.

if you want check for duplicate record in your db table, then try this like:

$q=mysql_query("select id from login where username=".$user_name);
$count=mysql_num_rows($q);
if($count==0)
{
// your insert code
}
else
{
echo "already exists in database.";
}

or unset the posted data like:

if (isset($_POST["Submit"])) 
{
// do your db insertions
unset($_POST); // clear everything
}

and also check some links:

Thank you :)..very much...
Only the first method works fine for me.

Also I had tried

unset($_POST);

It is clearing the array.But after resubmission again $_POST array is initialized with the posted data..
I can't use header().because I wanted to display some data on the same page.


But anyway my problem solved..thank u..

befor the ending of the insertion command declare a variable like

$NOMAORINSERTION=true;

and check this variable isset in the starting of the insertion command just like

if(isset[$_post['Submit']])
{
if(isset[$NOMOREINSERTION])
{
 //Insertion Command
}
$NOMOREINSERTION=true;
}

now,if you click the refresh button the the data willnot resubmit.. it's a simple logic

The db verify is needed or you can use different methods with tokens and sessions.
After inserting the value, you can redirect the user and when he hits F5 it will just refresh the page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.