0

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

4
Contributors
4
Replies
14
Views
6 Years
Discussion Span
Last Post by Szabi Zsoldos
0

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

Edited by Shanti C: n/a

1

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..

0

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

Edited by VICHUVAVA: correction

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.