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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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')

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.