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

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:
http://www.satya-weblog.com/2009/10/avoid-resubmitting-html-form.html

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.