Jump to content

php page refreshing


php_novice2007

Recommended Posts

Hi,

 

I'm writing a page which has a form, when the user clicks submit, it'll go to another page which deletes something from a mySQL database. I think if the user clicks refresh on the second page, it'll delete something again. How can I stop that? ie.. after that page is loaded (and the database entry deleted), if the user clicks refresh, nothing happens..

 

Thanks~!

Link to comment
Share on other sites

hmm where exactly would i put the header line..?

 

my code currently :

 

<html>
<body>
<?php
  if (isset($_POST['unitNum'])) {
    $unitNum = $_POST['unitNum'];
  }
  
  // establish link
$username="root";
$password="abcd";
$database="red1";
$dbtable = "commands";
$link=mysql_connect("localhost", $username, $password) or die("Cannot connect to database");

//select database
@mysql_select_db($database) or die("Unable to select database");

  $query="DELETE FROM `$dbtable` WHERE `$dbtable`.`unit_id` = $unitNum LIMIT 1;";
$result=mysql_query($query, $link) or die("Unable to delete");

//close link
mysql_close($link) or die("Unable to close database");

?>
Command for unit <?php echo $unitNum; ?> is deleted.<br><br>
<a href="commands.php">Go back to commands page</a>
</body>
</html>


Link to comment
Share on other sites

If you are deleting a record based on a unique id, such as 'record_id' then you won't have to worry about a refresh deleting another record.

 

DELETE FROM tbl WHERE record_id = 123

 

Will only delete the row that has the record_id of 123.  A refresh can't cause record_id 123 to be deleted twice.

Link to comment
Share on other sites

<?
$query="DELETE FROM `$dbtable` WHERE `$dbtable`.`unit_id` = $unitNum LIMIT 1;";
$result=mysql_query($query, $link) or die("Unable to delete");

//close link
mysql_close($link) or die("Unable to close database");
//Redirect Here
header( 'Location: comands.php');
ob_end_flush();  //I use this to prevent header/output issues
?>

 

A refresh could cause problems with adding records again.  Just put your header re-direct after you close your db connection.  Same as in the example above.

Link to comment
Share on other sites

Hi, I just tried adding your two lines into my code..

 


<?php
  if (isset($_POST['unitNum'])) {
    $unitNum = $_POST['unitNum'];
  }
  
  // establish link
$username="root";
$password="abcd";
$database="red1";
$dbtable = "commands";
$link=mysql_connect("localhost", $username, $password) or die("Cannot connect to database");

//select database
@mysql_select_db($database) or die("Unable to select database");

  $query="DELETE FROM `$dbtable` WHERE `$dbtable`.`unit_id` = $unitNum LIMIT 1;";
$result=mysql_query($query, $link) or die("Unable to delete");

//close link
mysql_close($link) or die("Unable to close database");

  //Redirect Here
  header( 'Location: comands.php');
  ob_end_flush();  //I use this to prevent header/output issues
?>

 

I'm getting these messages when I try to go to deleteCommand.php..

 

Warning: Cannot modify header information - headers already sent by (output started at I:\webpages\deleteCommand.php:2) in I:\webpages\deleteCommand.php on line 24

 

Notice: ob_end_flush() [ref.outcontrol]: failed to delete and flush buffer. No buffer to delete or flush. in I:\webpages\deleteCommand.php on line 25

 

 

Is the code meant to automatically go back to commands.php after the entry is deleted?

Link to comment
Share on other sites

Your warning message:

Warning: Cannot modify header information - headers already sent by (output started at I:\webpages\deleteCommand.php:2) in I:\webpages\deleteCommand.php on line 24

is issued because you're trying to change HTTP headers after the server has started to deliver the payload (HTML content) of the response. That's usually due to you already having used something like echo() before adding your redirect header.

 

The suggestion to issue a redirect to the browser, to do a GET, after your POST processing, is a well known design pattern that goes by a variety of names. This article might be a good starting point for you:

http://en.wikipedia.org/wiki/Post/Redirect/Get

 

Cheers,

 

Peeper.

 

Link to comment
Share on other sites

<?php
  if (isset($_POST['unitNum'])) {
    $unitNum = $_POST['unitNum'];
  }
  
  // establish link
$username="root";
$password="abcd";
$database="red1";
$dbtable = "commands";
$link=mysql_connect("localhost", $username, $password) or die("Cannot connect to database");

//select database
@mysql_select_db($database) or die("Unable to select database");

  $query="DELETE FROM `$dbtable` WHERE `$dbtable`.`unit_id` = $unitNum LIMIT 1;";
$result=mysql_query($query, $link) or die("Unable to delete");

//close link
mysql_close($link) or die("Unable to close database");

  ob_end_flush();  //I use this to prevent header/output issues
  header( 'Location: comands.php');
?>

 

Just remember any spaces at the top of the file counts as output. So any spaces or letters before <?php is considered output.

 

Also I would move the header outside of the output buffer statements.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.