geroido Posted August 6, 2008 Share Posted August 6, 2008 Hi all I have a webpage where the user can enter menu items(name, description, price etc.) and they are inserted into a database on clicking the 'save' submit button. It all works well. However, if I enter the information and click save, it works but if I then refresh the page, it puts the record into the database again and again and again if I keep refreshing which I don't want. Can you give me an idea what might be the problem. I'm not including any code now, just looking for ideas. I can submit the code if it helps. Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/ Share on other sites More sharing options...
MasterACE14 Posted August 6, 2008 Share Posted August 6, 2008 you want the person to only submit the form once then kill the form? use javascript. Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/#findComment-609433 Share on other sites More sharing options...
geroido Posted August 6, 2008 Author Share Posted August 6, 2008 Hi MasterACE14 Thanks but I don't want to kill the form because as soon as the record is inserted I am presenting the user with the form again to enter another record. They exit by using the 'cancel' submit button. Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/#findComment-609436 Share on other sites More sharing options...
MasterACE14 Posted August 6, 2008 Share Posted August 6, 2008 you could put whatever they insert the first time into a session variable, then when they submit the form again, check what they submit, against the session variable to make sure it doesn't match. Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/#findComment-609440 Share on other sites More sharing options...
geroido Posted August 6, 2008 Author Share Posted August 6, 2008 That sounds like a good idea. I'll try that. Thanks Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/#findComment-609479 Share on other sites More sharing options...
dannyb785 Posted August 6, 2008 Share Posted August 6, 2008 Any user can repeatedly submit a form if they know what they're doing. All they gotta do is revisit the page that processing was done on. What I'd suggest is before doing an insert, do a query searching for a row with all of the inputted credentials that are exactly the same. If you get a mysql_num_rows() > 0 then you can know if was refreshed and then skip the INSERT query. That's really the only way to protect refreshing from inserting multiple times. Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/#findComment-609483 Share on other sites More sharing options...
geroido Posted August 6, 2008 Author Share Posted August 6, 2008 Hi dannyb785 I think that's a good idea too. I have a couple of options now so thanks for that. Do you also think it would be possible to clear all the variables after insert so that the form validation would fail and therefore present the user with the form again for completion but as a result of failed validation would not do any insert? Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/#findComment-609514 Share on other sites More sharing options...
0siris Posted August 6, 2008 Share Posted August 6, 2008 yup, you should be able to do, after your insert statement set all the values to "", and then before inserting I presume you have a bit of code which prevents blank entries? Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/#findComment-609591 Share on other sites More sharing options...
geroido Posted August 6, 2008 Author Share Posted August 6, 2008 What kind of code would prevent a blank entry? Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/#findComment-609611 Share on other sites More sharing options...
lynxus Posted August 6, 2008 Share Posted August 6, 2008 $blaa = $_POST["blaa"]; $error = "0"; if ($blaa == "") { $error = "1"; } ?? Maybe thats completely not what you want ? Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/#findComment-609619 Share on other sites More sharing options...
0siris Posted August 6, 2008 Share Posted August 6, 2008 something like: foreach($_POST as $field => $value) { if($value == "") { $blankarray[]; } } if(sizeof($blankarray) == 0) { die("There are blank fields"); //put or do whatever you want here } Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/#findComment-609628 Share on other sites More sharing options...
geroido Posted August 6, 2008 Author Share Posted August 6, 2008 Thanks all I think I have enough to work with there. Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/#findComment-609655 Share on other sites More sharing options...
dannyb785 Posted August 6, 2008 Share Posted August 6, 2008 or just: unset($_POST); that should do the trick Link to comment https://forums.phpfreaks.com/topic/118408-problem-with-database-insert-on-refreshing-webpage/#findComment-610039 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.