|
Posted by noone on 02/24/06 17:11
Erwin Moller wrote:
> Mark wrote:
>
>
>>i'm using an html form to submit comments to a database. the form is on
>>the same page where the comments are being displayed. if you hit
>>submit, your comment appears as it should, but then if you hit refresh
>>there is a duplicate post. how might i fix this?
>
>
> Hi Mark,
>
> That has more to do with the browser than php.
>
> Look at your action-tag in your form.
> (It is very stange in your case by the way)
>
> If a form is submitted the browser expects a page as anser.
> In most cases this is the page the receiving script of the form generates.
>
> Now if you hit 'refresh' the browser assumes you want the same page
> refreshed, the one that was produced as a result of your posting.
>
> One simple way to 'fix' this (because nothing is wrong) is:
> page1.php contains form
> set the action to page1_process.php
>
> page1_process.php
> receives the form, does its stuff like databaseinserts.
> Do not create ANY output.
> End you script with something like this:
>
> $result = urlencode("Thanks for posting!");
> header ("Location: page1.php?result=".$result);
>
>
> And in page1.php you could display the result if it exists in the
> $_GET["result"].
>
> Now the browser gets the news that page1.php is the one to be displayed.
> Refreshing it will only refresh the page and not do a reposting.
>
> If you set up all your scripts like this:
> 1) You never get that annoying problem again
> 2) You neatly seperate processing pages from contentrich pages, thus
> avoiding long and bugprone scripts that do it 'all functionality at once'.
>
>
> Regards,
> Erwin Moller
Since you cannot prevent people from hitting the refresh button, you can
prevent the database from accepting it and generating an error to the
effect - you already did that.
you can create a primary key (PK) consisting of columns that you do not
want duplicated. I am assuming that a user name with a particular
email address can submit multiple comments with a unique id.
So your PK - or unique index depending on how you choose to resolve this
- would be name,email,id. If ID is not unique, then you would simple
let all 4 fields be a PK or have a unique index.
alter table comments add constraint pk_comments (name,email,comment,id);
This will prevent duplicate values. You should always check the status
of all database statements for success/failure/warning and handle it in
your code.
Navigation:
[Reply to this message]
|