if() and unset()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Klyve1
Forum Newbie
Posts: 14
Joined: Wed Sep 18, 2002 4:27 am
Location: Hove, UK

if() and unset()

Post by Klyve1 »

I'm using 'if' to check if a form has been submitted and if it has Insert stuff into the dB..... but if the page is reloaded it Inserts again!
I can't seem to drop the $Submit at all.
I've tried 'unset' but no joy, can anyone point me in the right direction?

Code: Select all

<?php
if ($Submit)
{
$sql = "INSERT INTO XYZ"; 

$inserted = mysql_query($sql);

unset ($Submit);
}
?>
Cheers

?>
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

lemme guess, your form and php processing is all in the same page. not a bad thing.
and the form is POSTing to itself and you if statement is catching the fact that it has been submitted. also, not a bad thing.
but then you try to refresh the page right after you submitted to it. this is where your problem lies. your browser is RE-submiting the form data and as a result $Submit is still going to be set.
hope that helps
User avatar
Klyve1
Forum Newbie
Posts: 14
Joined: Wed Sep 18, 2002 4:27 am
Location: Hove, UK

Post by Klyve1 »

Yes
Yes
& Yes!
Is there ANY way of either stopping this or dumping the $Submit variable prior to the script reading it?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

don't use the refresh button to re-request the page, rather click a link that will re-call this page. the problem does not lie in you php script, at least not inherintly, but rather the way that browsers handle posted forms in combination with the refresh button.
User avatar
Klyve1
Forum Newbie
Posts: 14
Joined: Wed Sep 18, 2002 4:27 am
Location: Hove, UK

Post by Klyve1 »

Thanks
I'd still like to find a way of turning this off though!!
I haven't tried it yet but there's going to be several pages with the same script on, will they auto-insert too or is it once again a refresh issue?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

whenever a php page displays a form and then processes the results of that form, you will always have this refresh problem. i can't think of anycheck that you could do that would fix this problem.
User avatar
Klyve1
Forum Newbie
Posts: 14
Joined: Wed Sep 18, 2002 4:27 am
Location: Hove, UK

Post by Klyve1 »

OK, thanks for you help
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

check the latest row of the database, and see if its the same as the row being inserted.
User avatar
Klyve1
Forum Newbie
Posts: 14
Joined: Wed Sep 18, 2002 4:27 am
Location: Hove, UK

Post by Klyve1 »

Yes it is, identical
Post Reply