For the following code I have a problem. When i fill in the form and submit it inserts a new database value. But when I refresh the page it does it again automatically with the same data, and keeps doing it whenever i refresh. How do i stop this?

<html>
<head>
<title>Using Default Checkbox Values</title>
</head>
<body>
<?php
// Include our login information
include('db_login.php');

$self = htmlentities($_SERVER['PHP_SELF']);
echo ('
<form method="POST" action="'.$self.'">
ID: <input type="text" name="id" /><br />
Name: <input type="text" name="name" /><br />
Comment: <input type="text" name="comment" /><br />
Date: <input type="text" name="date" /><br />
Other: <input type="text" name="other" /><br />
<input type="submit" value="Go!" /><br />
</form>
');

if(!$_POST['name'] == ''){
$query1 = "INSERT INTO `unknown`.`comments` (`id`, `user`, `comment`, `date`, `other`) VALUES (NULL, '$_POST[name]', '$_POST[comment]', '2010-10-23', '$_POST[other]')";
$result1 = mysql_query( $query1 );
$_POST['name'] = '';
unset($_POST);
}
// Assign the query
$query = "SELECT * FROM comments";
// Execute the query
$result = mysql_query( $query );
if (!$result){
die ("Could not query the database: <br />". mysql_error( ));
}
// Fetch and display the results
while ($result_row = mysql_fetch_row(($result))){
echo 'ID: '.$result_row[0] . '<br />';
echo 'Author: '.$result_row[1] . '<br /> ';
echo 'Comment: '.$result_row[2] . '<br /> ';
echo 'Date: '.$result_row[3] . '<br /> ';
echo 'Other: '.$result_row[4] . '<br /><br />';
}
//Close the connection
mysql_close($connection);
?>
</body>
</html>

Write something in a $_SESSION variable to detect that you already stored the form.

Or use the below code to send them to a new page that says whatever you want:

<?
header("Location: file.php");
?>

You must unset the $_POST somewhere in the end of the document (where you don't use it anymore):

<?php
unset($_POST['name'])
?>
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.