Jump to content

If page is refreshed data is written twice to database


rekha

Recommended Posts

Hi,

 

I have an HTML form that gets user info and when they press the submit button it will add the data to the database and then display the information they just added.  When the user is on the confirm page, if he pushes refresh it will resubmit the data to the database.  I cannot check to see if the row already exists, because it is okay for there to be multiple rows that are the same (just with a different time stamp).  What is the best way to prevent this from happening?  Thanks!

 

 

 

Regards

Rekha

http://hiox.org

To have an interim controller.

i.e.

form.html is the form

process.php is the php processing, which then does a header redirect to

display.html which displays the information

 

If the user refreshes display.html no more inserts will occur (because it's just an html page).

SO you got your page they entered the info and they press submit.

All data is sent to process.php... in process.php it puts all the data from the form in the db. when its done entering it you use the header command to send them to another page that shows them the data that they entered.

 

header function example

 

header('Location:www.yourpage.com/showdatatheyjustentered.php')

 

 

Pretty much as vico explained ^

 

I suspect what you're doing at the minute is using the processing stage to display the information input. i.e. you're using a 2-stage process. You want to seperate this:

 

e.g.

form.html

<form method="POST" action="process.php">
  <input type="text" name="name" />
  <input type="text" name="age" />
  <input type="submit" name="submit" value="process me" />
</form>

 

process.php

<?php
session_start();
if($_POST['submit']){
  $name = $_POST['name'];
  $age = $_POST['age'];

  // Do SQL stuff like inserting into DB here.
  $user = array('name'=>$name, 'age'=>$name);
  // Store user in the session.
  $_SESSION['user'] = $user;
}

header("Location: display.php");
?>

 

display.php

<?php
session_start();
echo "name:".$_SESSION['user']['name'];
echo "age:".$_SESSION['user']['age'];
?>

 

Hope that gives you a fuller example.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.