Hello All,

when I add/Delete data to my database (MySQL) using php, i discover that when i refresh the page my Add/delete script runs again and add (or attempt to delete) another copy of the record i added/deleted initially.

How can I prevent a block of php script from running twice after clicking refresh button. ( I dont want to have to check the database to see if the record already exist).

I understand that session can help me do this. Can somebody please tell me how ?


Emmanuel

simple. Soon after you execute your sql query, use header function to redirect the user to the same page. Eg.

<?php
$conn=mysql_connect("dbhost","dbuser","dbpass");
mysql_select_db("database");
if(isset($_POST['submit'])){
	$field1=$_POST['field1'];
	$field2=$_POST['field2'];
	$query="insert into rank (field1, field2) values (".$field1.",".$field2.")";
	mysql_query($query);
	 header("location: thispage.php");
}
?>
<html>
<body>
<FORM METHOD=POST ACTION="thispage.php">
	<INPUT TYPE="text" NAME="field1" ><br />
	<INPUT TYPE="text" NAME="field2" ><br />
	<INPUT TYPE="submit" name="submit" value="submit">
</FORM>
</body>
</html>

So, even if you refresh, it will do nothing, since $_POST is not set.

Hope it helps :)

Thank so much for your reply,

I think i know how to do that redirection thing in different ways.

1.

Thank you very much,

I think I know how to do this in various way

1. using the wonderful method you prescribed header("location: thispage.php");. Thanks to you.
2. this will also do the same echo "<meta http-equiv='Refresh' Content='0'; url='".$_SERVER."'>";3. or replace any of the above with Javascript's location.href=thisPage.php;

(I'm adding all this because it might be useful for some other person).

What I want to know is if PHP can do any of the above without refreshing the page at all. ie. clear the Post or GET after the data as been entered without doing a refresh. DOT NET inplements this functionality.

Thanks

Yep.. You are right about the redirecting methods. Umm.. but I am not aware of any other methods to clear data from $_POST/$_GET without refreshing the page.

u can try some ting
header(Cahe control : ....);
header(expires: some date);
See php manual...

i doont know , this may help u or not ...

Thank u so much nav33n. You really saved my day :)

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.