0

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>
4
Contributors
3
Replies
6
Views
6 Years
Discussion Span
Last Post by ErlendHL
0

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

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

Edited by Danny247: n/a

0

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

<?php
unset($_POST['name'])
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.