"egibberate" wrote in message
news:fYSdnbWy94w86C7YRVnytAA@bt.com...
> Hi,
>
> complete novice I'm afraid. When I input data into the form
>
> and click the submit button, the data is sent to the database twice ?
>
> ie. two identical records are logged into the table. Can anyone spot
> the
>
> flaw in the code below? Thanks in anticipation.
>
>
>
> if ($_POST) {
>
> $msg = "";
>
> foreach($_POST as $k => $v) {
>
> $v = trim($v) ;
>
> $$k = $v ;
>
> if ($v=="") {
>
> $msg = "Please fill in both fields";
>
> }
>
> }
>
> if ($msg=="") {
>
> $insert = "INSERT INTO tblshop_name
>
> (strshop_name, intshopcentre)
>
> VALUES ('$sn','$sc')";
>
> $link = mysqli_connect('localhost','root','blablabla','blablabla');
>
> if (!mysqli_query($link, $insert)) {
>
> $msg = "Error inserting data";
>
> } else {
>
> $msg = "Record successfully added";
>
> $sn = $sc = "";
>
> }
>
> mysqli_close($link);
>
> }
>
> echo "
$msg
";
>
> } else {
>
> $sn = $sc = "";
>
> }
>
> ?>
>
>
>
>
Hi again,
I copied the following two lines
from the apache server access log that
shows the data being sent two times,
39 seconds apart. Curious!
127.0.0.1 - - [21/Jan/2007:14:54:20 +0000] "POST /blablablabla.php HTTP/1.1"
200 2140
127.0.0.1 - - [21/Jan/2007:14:54:59 +0000] "POST /blablablabla.php HTTP/1.1"
200 2140
Re: Form submits the same record twice?
am 21.01.2007 16:58:34 von Shion
egibberate wrote:
> I copied the following two lines
> from the apache server access log that
> shows the data being sent two times,
> 39 seconds apart. Curious!
> 127.0.0.1 - - [21/Jan/2007:14:54:20 +0000] "POST /blablablabla.php HTTP/1.1"
> 200 2140
> 127.0.0.1 - - [21/Jan/2007:14:54:59 +0000] "POST /blablablabla.php HTTP/1.1"
> 200 2140
You didn't have a meta tag that reloads the page again?
If the times had been a lot closer to each other, then I would have suggested
double clicking or broken browser.
--
//Aho
Re: Form submits the same record twice?
am 21.01.2007 16:58:45 von egibberate
"egibberate" wrote in message
news:As-dnRDIM4IdGi7YnZ2dnUVZ8rKdnZ2d@bt.com...
> "egibberate" wrote in message
> news:fYSdnbWy94w86C7YRVnytAA@bt.com...
>> Hi,
>>
>> complete novice I'm afraid. When I input data into the form
>>
>> and click the submit button, the data is sent to the database twice
>> ?
>>
>> ie. two identical records are logged into the table. Can anyone spot
>> the
>>
>> flaw in the code below? Thanks in anticipation.
>>
>>
>>
>> if ($_POST) {
>>
>> $msg = "";
>>
>> foreach($_POST as $k => $v) {
>>
>> $v = trim($v) ;
>>
>> $$k = $v ;
>>
>> if ($v=="") {
>>
>> $msg = "Please fill in both fields";
>>
>> }
>>
>> }
>>
>> if ($msg=="") {
>>
>> $insert = "INSERT INTO tblshop_name
>>
>> (strshop_name, intshopcentre)
>>
>> VALUES ('$sn','$sc')";
>>
>> $link = mysqli_connect('localhost','root','blablabla','blablabla');
>>
>> if (!mysqli_query($link, $insert)) {
>>
>> $msg = "Error inserting data";
>>
>> } else {
>>
>> $msg = "Record successfully added";
>>
>> $sn = $sc = "";
>>
>> }
>>
>> mysqli_close($link);
>>
>> }
>>
>> echo "
$msg
";
>>
>> } else {
>>
>> $sn = $sc = "";
>>
>> }
>>
>> ?>
>>
>>
>>
>>
> Hi again,
> I copied the following two lines
> from the apache server access log that
> shows the data being sent two times,
> 39 seconds apart. Curious!
> 127.0.0.1 - - [21/Jan/2007:14:54:20 +0000] "POST /blablablabla.php
> HTTP/1.1" 200 2140
> 127.0.0.1 - - [21/Jan/2007:14:54:59 +0000] "POST /blablablabla.php
> HTTP/1.1" 200 2140
Hi yet again,
every time I click the refresh button on my browser
another identical record to the last, is entered into the table even tho
the form has nothing typed in the text boxes.
>
>
Re: Form submits the same record twice?
am 21.01.2007 17:12:13 von Paul Lautman
egibberate wrote:
> "egibberate" wrote in message
> news:As-dnRDIM4IdGi7YnZ2dnUVZ8rKdnZ2d@bt.com...
>> "egibberate" wrote in message
>> news:fYSdnbWy94w86C7YRVnytAA@bt.com...
>>> Hi,
>>>
>>> complete novice I'm afraid. When I input data into the form
>>>
>>> and click the submit button, the data is sent to the database
>>> twice ?
>>>
>>> ie. two identical records are logged into the table. Can anyone
>>> spot the
>>>
>>> flaw in the code below? Thanks in anticipation.
>>>
>>>
>>>
>>> if ($_POST) {
>>>
>>> $msg = "";
>>>
>>> foreach($_POST as $k => $v) {
>>>
>>> $v = trim($v) ;
>>>
>>> $$k = $v ;
>>>
>>> if ($v=="") {
>>>
>>> $msg = "Please fill in both fields";
>>>
>>> }
>>>
>>> }
>>>
>>> if ($msg=="") {
>>>
>>> $insert = "INSERT INTO tblshop_name
>>>
>>> (strshop_name, intshopcentre)
>>>
>>> VALUES ('$sn','$sc')";
>>>
>>> $link = mysqli_connect('localhost','root','blablabla','blablabla');
>>>
>>> if (!mysqli_query($link, $insert)) {
>>>
>>> $msg = "Error inserting data";
>>>
>>> } else {
>>>
>>> $msg = "Record successfully added";
>>>
>>> $sn = $sc = "";
>>>
>>> }
>>>
>>> mysqli_close($link);
>>>
>>> }
>>>
>>> echo "
$msg
";
>>>
>>> } else {
>>>
>>> $sn = $sc = "";
>>>
>>> }
>>>
>>>>
>>>
>>>
>>>
>>>
>> Hi again,
>> I copied the following two lines
>> from the apache server access log that
>> shows the data being sent two times,
>> 39 seconds apart. Curious!
>> 127.0.0.1 - - [21/Jan/2007:14:54:20 +0000] "POST /blablablabla.php
>> HTTP/1.1" 200 2140
>> 127.0.0.1 - - [21/Jan/2007:14:54:59 +0000] "POST /blablablabla.php
>> HTTP/1.1" 200 2140
>
> Hi yet again,
> every time I click the refresh button on my
> browser another identical record to the last, is entered into the
> table even tho the form has nothing typed in the text boxes.
Most browsers when you hit refresh on a posted page popup a warning that the
data will be re-submitted. I'm surprised that your one doesn't.
Re: Form submits the same record twice?
am 21.01.2007 17:47:37 von egibberate
>>egibberate wrote
>> Hi yet again,
>> every time I click the refresh button on my
>> browser another identical record to the last, is entered into the
>> table even tho the form has nothing typed in the text boxes.
>
> Most browsers when you hit refresh on a posted page popup a warning that
> the data will be re-submitted. I'm surprised that your one doesn't.
I've done some searching & it's normal behaviour for this to
happen when the refresh button is used. Pretty bad show
when credit card transactions are involved I shouldn't wonder.
Seems I need to take steps to to prevent it. Thanks for response.
>
>
Re: Form submits the same record twice?
am 21.01.2007 19:28:51 von Shion
egibberate wrote:
>>> egibberate wrote
>
>>> Hi yet again,
>>> every time I click the refresh button on my
>>> browser another identical record to the last, is entered into the
>>> table even tho the form has nothing typed in the text boxes.
>> Most browsers when you hit refresh on a posted page popup a warning that
>> the data will be re-submitted. I'm surprised that your one doesn't.
>
> I've done some searching & it's normal behaviour for this to
> happen when the refresh button is used. Pretty bad show
> when credit card transactions are involved I shouldn't wonder.
> Seems I need to take steps to to prevent it. Thanks for response.
It's really a browser problem and not directly linked to PHP and even less to SQL.
What you can do is to have script split up in two, in the main file you have
the form, you submit the data to a secondary page, if the data stored to the
database goes as it should, it redirects with a header() the user back to the
form page, if something goes badly, the make an output from the secondary page.
This way when you do refresh on the form, the data won't be saved a second time.
--
//Aho
Re: Form submits the same record twice?
am 23.01.2007 05:58:05 von Larry Anderson
Ive taken care of similar using session variables,
when you intially enter the form a session with a unique ID of the form
is recorded
$_SESSION['formcheck'] = date("Ymdhis");
and is added as a hidden value for POST
When the form loops back the DB checks to make sure the session and the
POST data match, if not it will exit without continuing
// this is after some code filtered $_POST['formcheck'] into
$forrmcheck
if($formcheck != $_SESSION['formcheck'] ){
header("Location: ../formexit.php");
}
If the POST was successful you can go on to validate data and if ready
to save - save it , if not, fall back to some url in the header line.
Now, once the data is valid, the form data is recorded and the formid
session value is intentially changed (thus preventing the back button
from possibly re-posting the saved data.)
By using a time value, if for some reason you re-do the entry (enter a
form, skip out before final validation start a different form and then
do a page-back twice) the first unfinished form entry will have the old
POST formid data and won't match the SESSION formid either so it does
not let the user mes up there either.
Re: Form submits the same record twice?
am 23.01.2007 19:23:07 von unknown
Post removed (X-No-Archive: yes)
Re: Form submits the same record twice?
am 23.01.2007 19:54:29 von Shion
Tom wrote:
> On Sun, 21 Jan 2007 14:16:01 -0000, egibberate wrote...
>> Hi,
>>
>> complete novice I'm afraid. When I input data into the form
>>
>> and click the submit button, the data is sent to the database twice ?
>>
>> ie. two identical records are logged into the table. Can anyone spot
>> the
>>
>> flaw in the code below? Thanks in anticipation.
>>
>>
>>
>> if ($_POST) {
>>
>> $msg = "";
>>
>> foreach($_POST as $k => $v) {
>>
>> $v = trim($v) ;
>>
>> $$k = $v ;
>>
>> if ($v=="") {
>>
>> $msg = "Please fill in both fields";
>>
>> }
>>
>> }
>>
>> if ($msg=="") {
>>
>> $insert = "INSERT INTO tblshop_name
>>
>> (strshop_name, intshopcentre)
>>
>> VALUES ('$sn','$sc')";
>>
>> $link = mysqli_connect('localhost','root','blablabla','blablabla');
>>
>> if (!mysqli_query($link, $insert)) {
>>
>> $msg = "Error inserting data";
>>
>> } else {
>>
>> $msg = "Record successfully added";
>>
>> $sn = $sc = "";
>>
>> }
>>
>> mysqli_close($link);
>>
>> }
>>
>> echo "
$msg
";
>>
>> } else {
>>
>> $sn = $sc = "";
>>
>> }
>>
>> ?>
>>
>>
>>
>>
>
> I normally use the "mysql_query" function. Might be worth trying that instead of
> the "mysqli_query" function and see if that makes a difference.
There aren't any difference between those two functions, his problem was that
he hit the reload button after have submitted the form, as he thought it would
refresh the form.