How to avoid duplicate entry in each page reload after PHP submit

I was using form submit using method=’POST’ and action=$_SERVER[‘PHP_SELF’]. I added data in my DB successfully but whenever I manually reloaded my page, it was a duplicate entry. using POST variables carefully, I was unable to resolve the problem.

here goes my code segment of samplepage.php:


if(isset($_POST["Add"]))
{
	$txtName=$_POST['txtName'];
        $txtKey= $_POST["txtKey"];
		
	//when submit button is add
	if($btnValue=="Add")
	{
		if($txtName<>"") //Permission Name can't be blank
		{
		    $query = "insert into Table(NAME, KEY) values('$txtName','$txtKey')" ;
		    $st = oci_parse($conn, $query);								 
	            $success = oci_execute($st,OCI_DEFAULT); // oci as db is oracle here
		    oci_commit($conn);
			
		    if($success ==1)
		    {
		     $SuccessMessage = "Data Added Successfully!";
		    }  
               
               }
        
        }
}

Later, I realized that no matter what how I check the post variables and use them carefully, the variables holds data and in manual reload of the page they were being inserted.

The solution is to RE-DIRECT the page!!.

So, the code segment becomes:

if($success ==1)
		{
		   $SuccessMessage = "Data Added Successfully!";
                   header('Location: samplepage.php');
		} 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s