Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I have this sub in my program, and it works almost right, but my problem is, if I refresh the page on the browser, it keeps reentering the information over and over again, the problem you can see on the code where it is but I can't figure it out on how to prevent it from doing it. Can someone take a look at it and let me know, please!
Thanks a lot!
sub save_entry { my $params = shift; my $entryID = $$params{id}; my $text= $$params{text}; #Added entry for the title here: $movie_title = $$params{movie_title}; $img_file = $$params{img_file}; print "1073 ********************************* $img_file ************ +**<br>"; my $user= $$params{user}; my ($mon, $year) = (); $entryID ? (($mon, $year) = (localtime($entryID))[4,5]) : (($mon, $year) = (localtime(time))[4,5]); $mon += 1; $year += 1900; mkdir("$user/$year", 0700); mkdir("$user/$year/$mon", 0700); #J:\ia\images\claims.gif $text =~ s/\n/<br>/g; $movie_title =~ s/\n/<br>/g; if($img_file =~/(.*?)(\w+|\W+)(\.)(\w+)/ig){ print "<br>##### 1=$1 - 2=$2 - 3=$3 - 4=$4 #####<br>"; $img_file = "$2$3$4"; print "<br>&&&& $img_file &&&&"; }else{print "**** NO *****";} print "<br><font color=white>1096&&&&$img_location$img_file&&&&$user +/$year/$mon&&&\$entryID=$entryID^^\$text=$text^^</font>"; #exit; if ($entryID) { if (open(update, "+<$user/$year/$mon/update.pl")) { local $/ = '@:'; my @entries = <update>; seek(update, 0, 0); truncate(update, 0); foreach (@entries) { chomp; next unless $_; my ($header) = split(/\n/, $_, 2); my ($ptime) = split(/:/, $header, 2); ($ptime eq $entryID) ? print update '@:', $ptime, ':', time, ':', $$params{show} ? 1 : 0, +"\n", $text, "\n", $movie_title, "\n", $img_file, "\n" : print update '@:', $_; } close update; } else { print_title({}, "Saving Problem"); print_note("Modifying entry: $!"); print_footer(); } } else { print "<br>1128 &&&& $img_file &&&&$$params{show} ? 1 : 0 , \$text=$ +text , $movie_title, $img_file,****"; if (open(update, ">>$user/$year/$mon/update.pl")) { # I tried here but it doesn't work, but here is where the uncessary up +date is, I know I just need to open the DB file that looks like the s +ample below, and if any part of it match don't update, but how? #unless($text=~/$text/ig){ print update '@:', time, '::', $$params{show} ? 1 : 0, "\n", $text, +"\n", $movie_title, "\n", $img_file, "\n"; #} close update; } else { print_title({}, "Saving Problem"); print_note("Writing entry: $!"); print_footer(); } } }

This is the text in the text db the the code works on it.
@:1107293018::1 Careus Joe careers.gif @:1107442029::1 Title content arrow.gif @:1107442514::1 My new title the content goes here arrow.gif @:1107442517::1 test more text for check arrow.gif

Replies are listed 'Best First'.
Re: File Updating Problem
by xorl (Deacon) on Feb 03, 2005 at 16:50 UTC
    This really isn't a perl question.

    When you refresh the browser it will normally resubmit any information to the server and thus cause your problem.

    There are a number of ways around this. Ususally I just put a big warning saying "DO NOT RELOAD THIS PAGE" or "ONLY CLICK SUBMIT ONCE OTHERWISE YOU WILL BE CHARGED TWICE."

    One other method you can use is to generate a time stamp on the form. When it is submitted if the difference between now and the timestamp is too great, give them a time out error. If data for that timestamp has already been entered then tell them they've already click submit once.

Re: File Updating Problem
by Anonymous Monk on Feb 03, 2005 at 17:16 UTC
    Well, the typical solution to prevent entering data twice in the database is to build an index on one or more of its columns, and mark the index as unique. This prevents duplicate data being entered. Considering you don't have an index of some sorts, you'll need to scan your file each time, to see whether the information has been entered already.

    Or perhaps you mean that you don't want the same user to enter information twice. In that case, use some form of authentication, and keep track who has submitted data, and who hasn't. Check before storing the data. Or index your data keyed on the user, and simply replace the data with the new one.