Jump to content


Photo

Preventing data being inserted into database again on refresh?


  • Please log in to reply
2 replies to this topic

#1 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 April 2006 - 02:53 PM

I have a script where I allow a user to add, edit or delete information from a database. I normally have three seperate sets of pages for each function, i.e. the form on one page, and the processing on another. In an attempt to make things cleaner I am integrating all of the scripts into one. My only problem is, if a user should decide to click refresh (for what ever reason) after perviously adding some info to the database, the info is added again, and again on any further refresh. Does anyone know of a method to prevent this, maybe by clearing the variables from the browser cache or something? - I don't know how this works technically at a browser level...

Heres a demo script to give you an idea of the kinda script I have:
<?php
if(isset($_POST['submit'])) {
    mysql_query("INSERT INTO `table` (`name`) VALUES ('$_POST[name]')");
}
?>
<form name="add" action="" method="post">
    <input type="text" name="name"/>
    <input type="submit" name="submit" value="Insert"/>
</form>


#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 20 April 2006 - 02:59 PM

Something along the lines of...



// Get the last row from the table before entering the new one.
SELECT * FROM table ORDER BY (unique id number field) DESC LIMIT 1

// Compare the content with the variables you've just submitted
if($db_variable=="$_POST[variable]")
{
// dont enter into the database
}
else
{
// DO ENTER into the database
}


wisewood: proven fact, I am both wise, and wooden.

#3 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 April 2006 - 03:14 PM

Sounds like that should be sufficient, thanks for that!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users