Jump to content

Archived

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

Kris

Preventing data being inserted into database again on refresh?

Recommended Posts

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:
[code]<?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>[/code]

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
Sounds like that should be sufficient, thanks for that!

Share this post


Link to post
Share on other sites

×

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.