Forum Moderators: coopster

Message Too Old, No Replies

Reload page keeps adding new records

         

Frank_Rizzo

9:49 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a form which is used to add / edit records in a mysql db.

This all works fine but after entering a record, and returning to a database view page, a user can hit reload page and a new record is added.

Clearly, the previous $_POST vars are still in the system and thus reloading the page runs the same script to add a new record.

New records should only be added when the submit button is pressed but after doing that successful, subsequent page refreshes repeats the procedure.

How do I prevent this?

dkin

10:12 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



have a checker of sorts, if you have a unique column in your db say no names can be the same then have something like

if ($_POST['name'] == $row['name'])
{
echo 'Sorry that name has already been inserted.';
}

Frank_Rizzo

11:16 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, not really.

There could be valid duplicates in some fields. I guess I need to check that a record does not contain the same info for all fields:

if ($_POST['field1'] == $row['field1'] && $_POST['field2'] == $row['field2'] && $_POST['field3'] == $row['field3'] && $_POST['field4'] == $row['field4'] )
{
//do nothing
} else {
// add new record
}

Just wondering if there is an easier way.

Is it possible to null the $_POST vars after a successful database insert? Something like:

(rough coding)

if($_POST['update_submitted']) {

$query="INSERT into db1 (field1...) values ($_POST['field'])";

$_POST['update_submitted'] = '';

dkin

11:58 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



but then when the form is reloaded would you not get empty spaces inserted into the database?

Frank_Rizzo

8:11 am on Sep 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You mean with the $_POST['update_submitted']=''; method?

It shouldn't produce a duplicate as the part which inserts the record won't be run:

if($_POST['update_submitted') {
// sql insert here
}

Unfortunately that method doesn't work. You can't force an overwrite of a $_POST. It will restore itself on reload.

Having had time to think of this I came up with two solutions.

The first is to set a flag on successful insert which turns off the hidden flag in the form:

if($_POST['update_submitted') {
// sql insert here
$already_did_this = 1;
} else {
$already_did_this = 0;
}

.
.
.
<FORM....
if($already_did_this == 0) {
print <input type="hidden" name="update_submitted">

That would work but there is an even easier way. All you have to do is to send a header to reload the page afer the data is inserted:

if($_POST['update_submitted') {
// sql insert here
header("location: [widgets.com...]
}

The header("location... bit forces a clean reload where all post and get vars are gone.

dcrombie

8:50 am on Sep 23, 2005 (gmt 0)



This is the best article I've found on the subject:
[theserverside.com...]
;)