Reputation: 121
I'm trying to create a register page and I am having problem when storing the data.
When I successfully register, it stores to database.
But when I reload it again, it automatically stores the same old data I typed to database.
How to solve this problem?
My simple php code looks like this
if (empty($fname)) {
echo '';
} else {
$success = ("
INSERT INTO customer
(FName, LName, Email, Password, MobileNum, PostalCode)
VALUES
('$fname','$lname', '$email', '$password', '$mobilenumber','$postalcode')
");
mysql_query($success);
echo 'success';
}
?>
Upvotes: 2
Views: 108
Reputation: 67
After success you may unset $_POST and if there is any output with echo or html you may use header to redirect to another page
if (empty($fname)) {
// echo '';
} else {
$success = ("INSERT INTO customer (FName, LName, Email, Password, MobileNum, PostalCode) VALUES ('$fname','$lname', '$email', '$password', '$mobilenumber','$postalcode')");
mysql_query($success);
unset($_POST);
header('Location: http://www.example.com/successPage.php')
/** if you don't able to use header, you may echo a javascrript redirect
echo '<script>window.location.href = "http://www.example.com/successPage.php"';</script>";
*/
// echo 'success';
}
Upvotes: 0
Reputation: 14523
You can check in database before inserting...
if(empty($fname)){
echo '';
}else{
$sql = "SELECT * FROM customer WHERE FName='$fname'";
$result = mysql_query($sql);
$numRows = mysql_num_rows($result);
if($numRows==0) {
$success=("INSERT INTO customer (FName, LName, Email, Password, MobileNum, PostalCode) VALUES ('$fname','$lname', '$email', '$password', '$mobilenumber','$postalcode')");
mysql_query($success);
echo 'success';
} else {
echo "Already exists";
}
}
Upvotes: 0
Reputation: 5171
Presuming that you're using an HTTP POST request when you "store" the data, the easiest way to ensure that re-loading the page doesn't do a re-POST of the same data is to do a redirect at the end of your script.
Instead of doing echo 'success'
do Header("Location: ?")
That way, the browser does a GET immediately after the POST. If the user refreshes the resulting page, they simply re-request the GET. (or do nothing at all if caching is in play).
Upvotes: 4