0

Here is my code of PHP

<?php
/* Attempt MySQL server connection. Assuming you are running MySQLServer with default setting (user 'root' with no password) */

$link = mysqli_connect("localhost", "root", "", "login");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 
// Escape user inputs for security
$fname = mysqli_real_escape_string($link, $_POST['fname']);
$lname = mysqli_real_escape_string($link, $_POST['lname']);
$address = mysqli_real_escape_string($link, $_POST['address']);
$phone = mysqli_real_escape_string($link, $_POST['phone']);
// attempt insert query execution
$sql = "INSERT INTO persons (firstname, lastname, address, phone) VALUES ('$fname', '$lname', '$address', '$phone')";
if(mysqli_query($link, $sql))
{
    echo "Records added successfully.";
 
} 
else
{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
 
// close connection
mysqli_close($link);
?>

This code is running fine but after refreshing the form it automatically submitted the old data on the database. Please help me to resolve the issue.

rahul
  • 776
  • 5
  • 30
Kumar Vivek
  • 314
  • 1
  • 12
  • 4
    when saved in db then redirect to other page. with `header(location:)` kind of stuff. – Jai Mar 19 '15 at 09:05
  • I don't want to redirect in to any location. I think it is due to cache or any thing else So can you describe me if i don't want to redirect it on other place. – Kumar Vivek Mar 19 '15 at 09:11
  • well then you have to follow the answer you got below. – Jai Mar 19 '15 at 09:13
  • Search for the POST-REDIRECT-GET pattern. Basically this means that every POST request is answered with a redirect, so the browser always does an additional GET request and never stores a POST request in its history. – martinstoeckli Mar 19 '15 at 09:28

5 Answers5

1

Before inserting the data, first check the database to see if the row already exists. e.g. SELECT * FROM persons WHERE firstname = $fname AND lastname = $lname AND address = $address AND phone = $phone If a row comes back, you shouldn't add it again.

Chris Walsh
  • 3,423
  • 2
  • 42
  • 62
0

When you reload your page, the submitted data is re-submitted and that's the reason it is re-adding the data to the database. To prevent this, you either re-direct after adding or use form tokens. Check this link: PHP form token usage and handling

Community
  • 1
  • 1
NaijaProgrammer
  • 2,892
  • 2
  • 24
  • 33
0

What happens is your page was requested with specific POST parameters. After you processed and saved to database your page outputs data to the client browser.

This browser remembers fetching this data with a POST request. So if you hit F5 you get a warning along the lines "This page was requested with data, if you continue this data will be resubmitted" Unless you ticked the option "I dont want to see this anymore"

To resolve this issue you need to have 3 pages:

Form

A page holding your form which the client fills in

Save

A page that validates and saves the data in database This will redirect to Endresult on success or Form on error using a header('Location:http://path/to/result');

Endresult

A page that displays the success message

This is the best way imo to prevent the resubmit of the data over and over again unless you do more expensive duplicate checks.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
0

Or just add a primary key.. either fname and lname combined or anything else.. It will make sure duplicates are not created.

Arun Babu
  • 108
  • 2
  • 13
0

We can either check overall database then insert the data in to the database or we can create primary key for the particular data.

Be assure that for stop resubmitting of the form you can redirect the submitted form after submitting.

Kumar Vivek
  • 314
  • 1
  • 12