Hi I was wondering how do I stop the data from being re-entered into the database table when a page is refreshed. I have heard that page redirection is best though I can't seem to get that working. Here is my code:
<?php
// If magic quotes is tuned on then strip slashes
if (get_magic_quotes_gpc()) {
foreach ($_POST as $key => $value) {
$_POST[$key] = stripslashes($value);
}
}
// Extract form submission
$name = (isset($_POST["name"]))?$_POST["name"]:"";
$email = (isset($_POST["email"]))?$_POST["email"]:"";
$submitAdd = (isset($_POST["submitAdd"]))?$_POST["submitAdd"]:"";
// Open connection to database
include("db_connect.php");
// Prepare data for database
$db_name = addslashes($name);
$db_email = addslashes($email);
// If form has been submitted, insert entry into database
if ($submitAdd) {
$sql = "INSERT INTO list
(name,email)
VALUES ('$db_name', '$db_email')";
$result = mysql_query($sql);
if (!$result) {
$message = "Failed to insert post. MySQL said " . mysql_error();
} else {
$message = "Success! $name, your details were added to our email list.";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title></title>
</head>
<body>
<?php if (isset($message)) { echo "<p class='message'>$message</p>"; } ?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>" onsubmit="return validate()">
<label for="name" class="formHeading">Name: </label>
<input type="text" name="name" size="40" />
<label for="email" class="formHeading">Email: </label>
<input type="text" name="email" size="40" />
<input type="Submit" name="submitAdd" value="Get Updated!"/>
</form>
</div>
</div>
</body>
</html>