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? 6 9167
relevant code:
// submit comment
if( $name && $comment )
{
$query = "INSERT INTO comments (name,email,com ment,id) VALUES
('$name','$emai l','$comment',$ id)";
mysql_query($qu ery,$db);
}
else if( $submit )
{
echo 'missing fields';
}
// display comments
$result = mysql_query("SE LECT * FROM comments WHERE id='$id' ORDER BY
timestamp DESC");
$num = mysql_numrows($ result);
for($i=0; $i<$num; $i++)
{
$name = mysql_result($r esult,$i,"name" );
$email = mysql_result($r esult,$i,"email ");
$comment = mysql_result($r esult,$i,"comme nt");
echo '<br />';
if( $email )
echo '<a href="mailto:'. $email.'">';
echo '<b>'.$name.': </b>';
if( $email )
echo '</a>';
echo '<br />'.$comment;
}
mysql_close();
// display form
echo '
<form action="?id='.$ id.'" method="POST">
<label for="name">Name </label>
<input name="name" type="text" /><br />
<label for="email">Ema il</label>
<input name="email" type="text" /><br />
<label for="comment">C omment</label>
<textarea name="comment"> </textarea><br />
<input name="submit" type="submit" value="Submit" />
</form>
';
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.p hp
page1_process.p hp
receives the form, does its stuff like databaseinserts .
Do not create ANY output.
End you script with something like this:
$result = urlencode("Than ks for posting!");
header ("Location: page1.php?resul t=".$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
Following on from Mark's message. . . relevant code:
// submit comment if( $name && $comment ) { $query = "INSERT INTO comments (name,email,com ment,id) VALUES ('$name','$ema il','$comment', $id)"; mysql_query($qu ery,$db);
You /have/ taken precautions to avoid SQL injection?
--
PETER FOX Not the same since the cardboard box company folded pe******@eminen t.demon.co.uk.n ot.this.bit.no. html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.dem on.co.uk>
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.p hp
page1_process.p hp receives the form, does its stuff like databaseinserts . Do not create ANY output. End you script with something like this:
$result = urlencode("Than ks for posting!"); header ("Location: page1.php?resul t=".$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,com ment,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.
Peter Fox wrote: You /have/ taken precautions to avoid SQL injection?
Not yet, but thanks. I forgot what the term for that kind of attack
was, I'll do some research on it.
Erwin Moller wrote: One simple way to 'fix' this (because nothing is wrong) is: page1.php contains form set the action to page1_process.p hp
page1_process.p hp receives the form, does its stuff like databaseinserts . Do not create ANY output.
ah... excellent. this gets rid of that annoying "resend information"
message on refresh too.
noone wrote: alter table comments add constraint pk_comments (name,email,com ment,id);
so this will make it so that ALL those values together can't be
identical with any other comment?
i was wondering how I might do this. I figured out how do put "unique"
on a single column, but that doesn't help me much.
thanks a lot for your help guys! this is great.
Mark wrote: Peter Fox wrote:
You /have/ taken precautions to avoid SQL injection?
Not yet, but thanks. I forgot what the term for that kind of attack was, I'll do some research on it.
Erwin Moller wrote:
One simple way to 'fix' this (because nothing is wrong) is: page1.php contains form set the action to page1_process.p hp
page1_process .php receives the form, does its stuff like databaseinserts . Do not create ANY output.
ah... excellent. this gets rid of that annoying "resend information" message on refresh too.
noone wrote:
alter table comments add constraint pk_comments (name,email,com ment,id);
so this will make it so that ALL those values together can't be identical with any other comment?
correct. the INSERT will fail. i was wondering how I might do this. I figured out how do put "unique" on a single column, but that doesn't help me much.
As I stated, you can also create unique indexes - the difference is the
error generated at the time of the failure - and how your code handles it.
Not knowing what other fields exist in this table makes the
recommendation of what to use a bit more difficult.
thanks a lot for your help guys! this is great.
any time. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Raj |
last post by:
Hi All,
I have a problem with trying to refresh the parent window from
child window in order to update data in the parent window.
The sequence of events are
1) I click a button in the parent window to open a child window thru
javascript
window.open
2) I have some functionality in the child window that changes the data
|
by: tasmisr |
last post by:
This is an old problem,, but I never had it so bad like this before,,
the events are refiring when clicking the Browser refresh button.
In the Submit button in my webform, I capture the server side click
event and I update session information to track the visibilty of some
panels in a wizard type navigation ("Next >>" and "<< Back" buttons)..
but the refresh button makes another event firing and takes the whole
thing to a Choes..
...
|
by: Xavier Osa |
last post by:
Hi,
I have an ASP.Net web page that you can download a file. As
Fergunson's problem, it prompts twice dialog boxes only if I select
Open button. If I select Save button, it prompts once.
I'm using W2000KS & IE6 sp1 & VS.NET 2003.
If I change method="post" by method="get" form attribute, it works
fine.
|
by: Brad White |
last post by:
Overview:
I have a custom web app that has an 'Inbox' that refreshes every 30 seconds.
One user uses Outlook to host the web page.
Using IE, the refresh works fine. If the user is working in another window,
the
web page quietly refreshes in the background.
Hosted in Outlook, the refresh causes Outlook to come to the front on
every refresh. Or in XP, causes the toolbar icon to flash.
|
by: IkBenHet |
last post by:
Hello,
Currently I am using a large input form on a website that is based on
ASP and JavaScript.
Depending on the values that are filled in by the user the forms does a
refresh and makes other input fields available to fill in. I use the
JavaScript OnChange function (=clientside) that creates a querystring
and does a refresh of the page. It works fine.
| |
by: SamSpade |
last post by:
picDocument is a picturebox
When I do picDocument.Invalidate() the box paints.
But if instead I do picDocument.Refresh() the box does not paint.
What does Refresh do. I guessed it did an Invalidate and an Update.
Can someone shed some light?
|
by: Fred Nelson |
last post by:
Hi:
I have a VB.NET web application and I need to find a way to cause a page
refresh from within my application.
Does anyone know how to force the browser to refresh the current page?
Thanks very much for your help!
Fred
|
by: SimonZ |
last post by:
When user insert some data on page and click save button this data is
inserted to database.
If he clicks refresh button, the data is saved again each time when he
clicks this button.
I would like to prevent refresh from that page, so I inserted the following
lines in code behind on page_load event:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
|
by: Aegixx |
last post by:
Ok, extremely wierd situation here:
(I'll post the code below, after the explanation)
I've got a Windows application (.NET 3.5) that has a single Form with
a DataGridView embedded. The user presses a button to do a SQL query
(fill a DataSet). Before firing the query, I disable the grid/buttons
and turn on UseWaitCursor. After it completes, I re-enable the grid/
buttons, refresh the dataset, then turn off the UseWaitCursor. If I...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |