php_novice2007 Posted April 29, 2007 Share Posted April 29, 2007 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~! Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/ Share on other sites More sharing options...
lalabored Posted April 29, 2007 Share Posted April 29, 2007 Use header() on that other page and send them to another page? http://www.php.net/manual/en/function.header.php Oh or you can unset() the post or get variables from the form they submit. http://www.php.net/manual/en/function.unset.php Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/#findComment-240802 Share on other sites More sharing options...
php_novice2007 Posted April 29, 2007 Author Share Posted April 29, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/#findComment-240805 Share on other sites More sharing options...
tcollie Posted April 29, 2007 Share Posted April 29, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/#findComment-240806 Share on other sites More sharing options...
php_novice2007 Posted April 29, 2007 Author Share Posted April 29, 2007 hmm true.. what about if I'm trying to add records to my database? Then if I click refresh would it tries to add it again? Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/#findComment-240807 Share on other sites More sharing options...
tcollie Posted April 29, 2007 Share Posted April 29, 2007 <? $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. Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/#findComment-240808 Share on other sites More sharing options...
php_novice2007 Posted April 29, 2007 Author Share Posted April 29, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/#findComment-240826 Share on other sites More sharing options...
php_novice2007 Posted April 29, 2007 Author Share Posted April 29, 2007 bump.. Anybody? I still can't get it working.. And now I'm writing a page which toggle some values in the database and now if I click refresh it gets toggled again, ie back to original.. I've tried unsetting the $_POST variable, but that doesn't seem to work.. Please help.. Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/#findComment-240993 Share on other sites More sharing options...
tcollie Posted April 29, 2007 Share Posted April 29, 2007 You need to add ob_start(); at the beginning of your code. Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/#findComment-241024 Share on other sites More sharing options...
the_oliver Posted April 29, 2007 Share Posted April 29, 2007 Alternitivly after querying set $_POST['unitNum'] = 0. That query will have no data to search for. Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/#findComment-241042 Share on other sites More sharing options...
peeper Posted April 29, 2007 Share Posted April 29, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/#findComment-241070 Share on other sites More sharing options...
the_oliver Posted April 29, 2007 Share Posted April 29, 2007 Solution to the headers already being sent would also be to use a HTML refresh, at it can be used at anystage. EG: print "<meta http-equiv=\"refresh\" content=\"0;URL=redirecttome.php\">"; Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/#findComment-241091 Share on other sites More sharing options...
per1os Posted April 29, 2007 Share Posted April 29, 2007 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/49150-php-page-refreshing/#findComment-241101 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.