0

Hello all

I have a page in php that INSERTs data in a database via a query. My problem is that when you refresh the page the data is INSERTed again. How can I stop this.

Thanks in advance

4
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by metalix
0

hi,

how is the data getting to that page (post or get)

how are your refreshing the page?

0

After inserting redirect to another page which have link to insert page
That is page home.php have link to insert.php if one click link it takes him to insert.php and after inserting redirect to home.php

0

data is getting to the new page via post. if the user hits the refresh button on his/her browser the data is entered in twice.

0
<?php
include 'connect.php';
$id=$_POST['id'];
if(!isset($id))
{
echo "<script>alert('eMop ID not Set')</script>";
}

$chkbox = $_POST['chk'];
$imp=$_POST['imp'];
$step= $_POST['step'];
$start=$_POST['start'];
$finish = $_POST['finish'];
$duration = $_POST['dur'];
$status= $_POST['status'];


if(isset($imp)){
	
	foreach($imp as $a => $b){
	$query_all = "INSERT INTO test (emop_id, implementor, steps, start, finish, duration, status) VALUES ('$id', '$imp[$a]','$step[$a]','$start[$a]','$finish[$a]','$duration[$a]', '$status[$a]')";
	mysql_query($query_all) or die('Error with TEST query, query failed:'. mysql_error());
	}
}
0

Add redirection after inserting

if(isset($imp)){
	
	foreach($imp as $a => $b){
	$query_all = "INSERT INTO test (emop_id, implementor, steps, start, finish, duration, status) VALUES ('$id', '$imp[$a]','$step[$a]','$start[$a]','$finish[$a]','$duration[$a]', '$status[$a]')";
	mysql_query($query_all) or die('Error with TEST query, query failed:'. mysql_error());
	}
  header(Location:"home.php");//do redirection here
}

Edited by Stefano Mtangoo: forgot code tags

0

What is i write a SELECT query also. could i still pass data to home or another page using header(Location:"home.php"); ?

0

you can still pass values using the url in redirect.

however you can simply use sessions like:

if (!isset($_SESSION['form'])){
//handle your form
$_SESSION['form'] = '';
}
This question has already been answered. 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.