Keeps inserting to mysql on browser reload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpHappy
Forum Commoner
Posts: 33
Joined: Wed Oct 26, 2011 1:58 pm

Re: Keeps inserting to mysql on browser reload

Post by phpHappy »

Any reload of a page that accepts form data resends that data

$_POST "post" method isn't seen in a URL like $_GET "get" method but it is there and a reload has that data
insert if not exists
Amanda1998
Forum Newbie
Posts: 23
Joined: Tue Dec 13, 2011 10:25 am

Re: Keeps inserting to mysql on browser reload

Post by Amanda1998 »

Each time you make a query to MySQL, you can give an >> exit(); (prevents continuous execution), and after inserting data you do a redirect:
This is your form.php

Code: Select all

<?php
	if (isset($_POST['submit']))
	{
	ob_start();
   	include('chat.php');
	ob_end_clean();
	}
?>
<html bs goes here>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      Message: <input type="text" name="message" />
      <input type="submit" />
</form>
</html bs end here>
And here on >> chat.php file, you do what you need to do with the data and at the end, you do

Code: Select all

<?php
/** blah, blah, your code  **/
/** blah, blah, your code  **/


/** and at the end **/
echo '<meta http-equiv="REFRESH" content="0; url=chat.php" />';
?>
Last edited by Amanda1998 on Tue Dec 20, 2011 1:45 pm, edited 2 times in total.
Post Reply