Gossamer Forum
Home : General : Perl Programming :

Deleting lines in a database

Quote Reply
Deleting lines in a database
In my admin for a calendar for a scout site I want to allow the admin to delete more than one events at a time. I have coding but for some reason it deletes the ENTIRE calendar database file. If you can find the bug and fix it that would be great. Here is the coding having to do with opening the database and deleting it.
....
if ($input{action} eq "delete_entries") {
print "Content-type: text/html\n\n";
$description="Delete entries";
&delete_entries;
}
....
sub calendar {
#######################################
# Define the variables for the script #
#######################################
$database="$path/databases/calendar.txt";

##################################################
# Check to see if there was a database specified #
##################################################
if ($input{'database'} eq ''){
$db=$database;
}else{
$db=$input{'database'};
}

#################################################
# Get the origional database data & buffer data #
#################################################
open (ORGDB,"<$database");
@ODB=<ORGDB>;
close (ORGDB);

#################################################################
# Open the database to write data, changing the neccesary lines #
#################################################################
print "Content-type: text/html\n\n";
print "
<html>
<head><title>Troop 360 Calendar</title>
<head>
<body>
<font face='Arial' size='2'>
<h1><i>Troop 360 Calendar</i></h1>
<table width='100%' border='0' align=center>
<tr align=center><td bgcolor='#F2C973'><font size='2'>Action</font></td><td bgcolor='#F2C973'><font size='2'>Date</font></td><td bgcolor='#F2C973'><font size='2'>Type</font></td><td bgcolor='#F2C973'><font size='2'>Location</font></td><td bgcolor='#F2C973'><font size='2'>Remarks</font></td><td bgcolor='#F2C973'><font size='2'>Additional Important Comments</font></td></tr>
<form action='admin.cgi' method='post'>
";
foreach $rec (@ODB){
chomp($rec);
($date,$type,$location,$remarks,$comment1,$comment2,$comment3,$comment4,$comment5)=split(/\|/,$rec);
print "<tr align=center><td><font size='-2'><a href='admin.cgi?edit=edit_calendar&date=$date&type=$type&location=$location&remarks=$remarks&comment1=$comment1&comment2=$comment2&comment3=$comment3&comment5=$comment5&comment4=$comment4'>Edit</a> - <input type=\"checkbox\" name=\"event\" value=\"$rec\"></font></td><td><font size='-2'>$date</font></td><td><font size='-2'>$type</font></td><td><font size='-2'>$location</font></td><td><font size='-2'>$remarks</font></td><td><font size='-2'>$comment1$comment2$comment3$comment4$comment5</font></td></tr>\n";
}
print "
<input type=\"hidden\" name=\"action\" value=\"delete_entries\">
<tr><td><input type=\"submit\" value=\"Delete selected\"></form></td></tr>
</table>

<center><font size='2'><a href='http://$url/admin/admin.cgi?addtocalendar'>Add an event</a></font></center>
</font>

";
&copyright;
print "
</body>
</html>
";
}
....
sub parse_form {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if (length($buffer) < 5) {
$buffer = $ENV{QUERY_STRING};
}
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$input{$name} = $value;
}
}
....
sub error {
($error) = @_;
print $a->header();
print $a->start_html(-title=>'Error!', -bgcolor=>$bgcolor, -text=>$text, -link=>$link, -alink=>$alink, -vlink=>$vlink);
print "I am sorry, but $error. Go <a href=\"login.cgi\">back and correct the error.\n";
print $a->end_html;
exit;
}

sub delete_entries {
&flock($path."/databases/calendar.txt");
open (list, "<$path/databases/calendar.txt") or &error("Unable to open the data file for reading");
@list=<list>;
close(list);
&unflock($path."/databases/calendar.txt");

##############################################
# store all selected addresses into an array #
##############################################


@pairs2 = split(/&/, $buffer);
$count=0;
foreach $pair2 (@pairs2) {

($date,$type,$location,$remarks,$comment1,$comment2,$comment3,$comment4,$comment5) = split(/\|/, $pair2);

if ($field_name eq "event") {
@entries[$count]="$field_value";
$count++;
}

}

#########################################################
# use a foreach loop to delete the selected entry files #
#########################################################
foreach $entry(@entries) {
$count=0;
foreach $list(@list) {

if ($list =~ /$entry/i) {
splice(@list, $count, 1);

print "$entry was removed.
";
}
$count++;
}


}

&flock($path."/databases/calendar.txt");
open (list, ">$path/databases/calendar.txt") or &error("Unable to write to the data file");
print list @list;
close(list);
&unflock($path."/databases/calendar.txt");


exit;
}

sub flock
{
$lock_file="$path/databases/calendar.txt";
local ($lock_file) = @_;
local ($timeout);

$timeout=20;
while (-e $lock_file &&
(stat($lock_file))[9]+$timeout>time)
{ sleep(1);}

open LOCK_FILE, ">$lock_file"
or &error("Unable to create $lock_file");
}

sub unflock
{
$lock_file="$path/databases/calendar.txt";
local ($lock_file) = @_;

close(LOCK_FILE);
unlink($lock_file);
}

Quote Reply
Re: Deleting lines in a database In reply to
Oh yeah in case you dont notice the "...." is where there is coding that does other stuff in between. Thanks again everyone.