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

In my main body I have a statement that checks to see if once a reservation has been made whether the user has selected delete to remove the entry from the database. I broke this down into a small perl program instead of a CGI one and ran it from the command prompt without the interace. It runs fine as long as you as you give it specific values instead of user input. I think the problems is with the display_appointments() function which instead of creating separate instances of "delete" only sees a single instance no matter how many reservations are made. Thus, when you attempt to remove an item by pressing delete you get rid of all the items currently appearing on the page. This is extremely annoying as the only time you see the rest of the appointments again is if you reserve another conference room. I thought maybe I could create a conference_delete object and just delete that specific one each time. However, I thought someone might be able to point me in a better direction. I apologize if this might seem be easy for most folks, but I'm a perl n00b. BTW, this is a modification of an opensource calendar project from http://www.fuzzymonkey.org Any help would be great. Thanks!! The code is as follows:
use Date::Manip; use DBI(); use CGI qw(:standard); require "../common.pl"; require "../sitevariables.pl"; $cur = CGI->new(); $mode = "default"; if($cur->param("mode")) { $mode = $cur->param("mode"); } $day = $cur->param("day"); $month = $cur->param("month"); $year = $cur->param("year"); $sec = $cur->param("sec"); $min = $cur->param("min"); $hour = $cur->param("hour"); $end_min = $cur->param("end_min"); $end_hour = $cur->param("end_hour"); $end_am_pm = $cur->param("end_am_pm"); $am_pm_day = $cur->param("am_pm_day"); $laptop = $cur->param("laptop"); $conf_room = $cur->param("conf_room"); @am_or_pm = ("","AM","PM"); @end_am_or_pm = ("","AM","PM"); ("","Exec","06","11","16","31","35","47","none"); @pretty_months = ("","January","Febuary","March","April","May","June", +"July","August","September","October","November","December"); $content .= "<table width=100% border=0><tr><td width=50%><a href=\".. +/index.cgi?month=$month&year=$year\"><-- back to calendar</a></td>"; $content .= "<td width=50% align=right>&nbsp;</td></tr></table>"; if($cur->param("Delete")){ $content .= delete_appointment($day,$month,$year,$hour,$min,$conf_ +room,$conf_rooms); } $content .= print_form(); if($cur->param("conf_room")){ $content .= update_day(); } $content .= display_appointments($day,$month,$year,$hour,$min,$am_pm_d +ay,$end_hour,$end_min,$end_am_pm,$conf_room); open (TEMPLATE, "../template.html")||dienice("Content-type: text/html\ +n\nCould not find template"); while (<TEMPLATE>){ $page .= $_; } $page =~ s/<!-- ?content ?-->/$content/ig; close (TEMPLATE); print "Content-type: text/html\n\n$page"; ############ BEGIN SUBROUTINES ################################# sub print_form { my $return; my ($sec,$min,$hr,$mday,$mon,$year_localtime,$wday,$yday,$isdst) = + localtime(time()); my $current_year = $year_localtime + 1900; my $current_month = $mon + 1; my $current_sec = $sec; my $current_min = $min; my $current_hour = $hr; $return .= " <center> <form method=post> <table bgcolor=$edittitlebg cellpadding=0 cellspacing=1 border=0 width +=375><tr><td> <table bgcolor=$editborder cellpadding=4 cellspacing=0 width=100%> <tr bgcolor=$edittitlebg><td><font color=$edittitlefont>Reserve a Conf +erence Room</font></td></tr> <tr><td>Date&nbsp;&nbsp;&nbsp;&nbsp; <select name=month>"; for($index=1;$index < 13;$index++){ if($month){$current_month=$month;} if($current_month==$index){ $return .="<option value=$index selected>$pretty_months[$i +ndex]</option>\n"; }else{ $return .="<option value=$index>$pretty_months[$index]</op +tion>\n"; } } $return .= " </select> &nbsp;&nbsp;&nbsp;&nbsp; <select name=day>"; for($index=1;$index < 32;$index++){ if($day){$mday=$day; $current_day = $day;} if($index == $mday){ $return .="<option value=$index selected>$index</option>\n +"; }else{ $return .="<option value=$index>$index</option>\n"; } } $return .= " </select> &nbsp;&nbsp;&nbsp;&nbsp; <select name=year>"; if($year){$current_year = $year; } for($index=$current_year-5;$index < $current_year+5;$index++){ if($current_year==$index){ $return .="<option value=\"$index\" selected>$index</optio +n>\n"; }else{ $return .="<option value=$index>$index</option>\n"; } } $return .= " </select><br> <br> </select> Start Time&nbsp;&nbsp;&nbsp;&nbsp; <select name=hour>"; if($hour){$current_hour = $hour; } #for($index=0;$index < 24;$index++){ for($index=1;$index < 13;$index++){ if($current_hour==$index){ $return .="<option value=\"$index\" selected>$index</optio +n>\n"; }else{ $return .="<option value=$index>$index</option>\n"; } } $return .= " </select> : <select name=min>"; if($min){$current_min = $min;} for($index=0;$index < 60;$index++){ if($current_min==$index){ if($index < 10) { $return .="<option value=\"$index\" selected>0$index</ +option>\n"; }else{$return .="<option value=\"$index\" selected>$index< +/option>\n"; } }else{ if($index < 10) { $return .="<option value=$index>0$index</option>\n"; }else{ $return .="<option value=$index>$index</option>\n"; + } } } $return .= " </select>&nbsp;&nbsp;&nbsp;&nbsp;<select name=am_pm_day>"; if(am_pm_day) { for($index=1;$index < 3; $index++) { if($index == 1) { $return .="<option value=$index selected>$am_or_ +pm[1]</option>\n"; } if($index == 2) { $return .="<option value=$index selected>$am_or_ +pm[2]</option>\n"; } } } $return .= " </select> <br><br> </select>End Time&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <select name=endy_hour +>"; if($endy_hour){ $end_hour = $endy_hour; } #for($index=0;$index < 24;$index++){ for($index=1;$index < 13;$index++){ if($current_hour==$index){ $return .="<option value=\"$index\" selected>$index</optio +n>\n"; }else{ $return .="<option value=$index>$index</option>\n"; } } $return .= " </select> : <select name=endy_min>"; if($endy_min){ $end_min = $endy_min; } for($index=0;$index < 60;$index++){ if($current_min==$index){ if($index < 10) { $return .="<option value=\"$index\" selected>0$index</ +option>\n"; }else{$return .="<option value=\"$index\" selected>$index< +/option>\n"; } }else{ if($index < 10) { $return .="<option value=$index>0$index</option>\n"; }else{ $return .="<option value=$index>$index</option>\n"; + } } } $return .= " </select>&nbsp;&nbsp;&nbsp;&nbsp;<select name=end_am_pm>"; if(end_am_pm) { for($index=1;$index < 3; $index++) { if($index == 1) { $return .="<option value=$index selected>$end_am_or_pm[1 +]</option>\n"; } if($index == 2) { $return .="<option value=$index selected>$end_am_or_pm[2 +]</option>\n"; } } } $return .= " </select> <br><br> Conference Room </select> &nbsp;&nbsp;&nbsp;&nbsp; <select name=conf_room>"; for($index=1;$index < 9;$index++){ $return .="<option value=$index selected>$conf_rooms[$inde +x]</option>\n"; } $return .= " </select><br> <!--<input type=radio name=type value=weekly>every week<br> <input type=radio name=type value=biweekly>every week<br>--> <br>Please input the following information<br> <FORM METHOD=POST ACTION=/var/www/cgi-bin/sqlcalendar/protected/update +.cgi> <PRE> <tr><td> First Name:&nbsp;<INPUT NAME=$fname SIZE=34><br><br> Last Name:&nbsp;<INPUT NAME=$lname SIZE=34><br><br> Client:&nbsp;<INPUT NAME=$client SIZE=34><br><br> <input type=submit value=Reserve> </td></tr> </font> </PRE> </td></tr> </table> </td></tr></table> </form> </center> "; return $return; } sub display_appointments($$$){ $db = DBI->connect("DBI:mysql:database=dbname;host=localhost", mys +ql, ""); if(! $db) { print "!!Connection Failed!!<P>"; die(); } my $return; $return .= "<p><center> <table bgcolor=$edittitlebg cellpadding=0 cellspacing=1 border=0 w +idth=375> <tr><td> <table bgcolor=$editborder cellpadding=4 cellspacing=0 width=1 +00%> <tr bgcolor=$edittitlebg><td> <font color=$edittitlefont>Today's Reserved Conference Roo +ms</font> </td></tr> </table> </td></tr> </table> <table> <tr> <td> "; $newDate = "$year:$month:$day"; $query = "SELECT confName, start_time, end_time FROM Rese +rves_Conf WHERE resDate='$newDate'"; $select = $db->prepare($query); $select->errstr, "\n"; $rv = $select->execute() or die "Couldn't execute query ' +$query' \n"; $select->bind_columns(undef, \$confName,\$start_time,\$en +d_time); while($select->fetchrow_arrayref()) { if($confName eq "Exec") { $return .= "<left><form method=post><input type=sub +mit value=Delete> $confName &nbsp;&nbsp;&nbsp;&nbsp; $start_time &nbsp;&nbs +p;&nbsp;&nbsp; $end_time\n </form></left><br>"; } else { $return .= "<left><form method=post><input type=sub +mit value=Delete> $confName &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp +; $start_time &nbsp;&nbsp;&nbsp;&nbsp; $end_time\n </form></left><br>"; } } $return .= " </td> </tr> </table> </center></p>"; return $return; } sub update_day(){ my $update_hour= $cur->param("hour"); my $end_hour= $cur->param("e +ndy_hour"); my $update_min= $cur->param("min"); my $end_min= $cur->param("endy +_min"); my $update_year= $cur->param("year"); my $update_ampm = $cur->para +m("am_pm_day"); my $update_month= $cur->param("month"); my $update_endam = $cur->p +aram("end_am_pm"); my $update_day= $cur->param("day"); my $update_conf_room = $cur-> +param("conf_room"); my $update_conf_rooms = $cur->param("conf_rooms"); $db = DBI->connect("DBI:mysql:database=dbname;host=localhost", mys +ql, ""); if(! $db) { print "!!Connection Failed!!<P>"; die(); } if($conf_rooms[$update_conf_room] eq "none") { $resCurrent = 1;} else { if(($update_hour == 12) && ($am_or_pm[$update_ampm] eq 'AM')) { $update_hour = 0; } if(($end_hour == 12) && ($end_am_or_pm[$update_endam] eq 'AM')) { $end_hour = 0; } if(($update_hour != 12) && ($end_hour != 12)) { if($am_or_pm[$update_ampm] eq 'PM') { $update_hour = $update_ho +ur + 12; } if($end_am_or_pm[$update_endam] eq 'PM') { $end_hour = $end_hou +r + 12; } } if ($resCurrent == 1) { print "!! Conference Room: $conf_room, has been reserved during + this time period !! \n<P>" } else { $totime = "$update_hour:$update_min:00"; $endtime = "$end_hour:$end_min:00"; $resrvDate = sprintf("%04d-%02d-%02d", $update_year, $update_mo +nth, $update_day); $statement = qq[insert into Reserves_Conf(confID,confName,start +_time,end_time, resDate,id) values('$confID','$conf_rooms[$update_co +nf_room]', '$totime','$endtime','$resrvDate','100000')]; $insert = $db->prepare($statement) or die "Couldn't prepare the + query:", $insert->errstr, "\n"; $rv = $insert->execute or die "Order insert failed: ", $insert- +>errstr, "\n"; } } $db->disconnect(); } sub delete_appointment($$$$){ my $update_conf_rooms = $cur->param("conf_rooms"); my $update_conf +_room = $cur->param("conf_room"); my $update_hour = $cur->param("hour"); my $update_min = $cur->para +m("min"); my $update_year = $cur->param("year");my $update_month = $cur->par +am("month"); my $update_day = $cur->param("day"); $db = DBI->connect("DBI:mysql:database=dbname;host=localhost", mys +ql, ""); if(! $db) { print "!!Connection Failed!!<P>"; die(); } $query = "select confName from Reserves_Conf WHERE confName = '$up +date_conf_rooms[$update_conf_room]'"; $select = $db->prepare($query); $select->errstr, "\n"; $rv = $select->execute() or die "Couldn't execute query '$query' \ +n"; $select->bind_columns(undef, \$confName); $newDate = sprintf("%04d-%02d-%02d", $update_year, $update_month, +$update_day); $newTime = "$update_hour:$update_min:00"; $statement = "DELETE FROM Reserves_Conf WHERE resDate='$newDate' A +ND start_time='$newTime'"; $statement = $db->prepare($statement); $statement->errstr, "\n"; $rv = $statement->execute() or die "Couldn't execute query '$query +' \n"; $db->disconnect(); }

20040729 Edit by ysth: add readmore tags

Replies are listed 'Best First'.
Re: Form Post Problems
by friedo (Prior) on Jul 30, 2004 at 04:57 UTC
    You do not appear to be using strict or warnings. This is your major problem.

    As for the rest, I don't think you'll find too many people here willing to go through hundreds of lines of code to solve your problems. You'll need to do some work on your own to isolate the error to a manageable chunk of code.

    For developing large web applications, I reccomend using a simple framework that allows you to break up your code into usable chunks, such as CGI::Application.

      I had actually isolated the problem, but was then told that I should post the code in its entirety in Seekers of Perl Wisdom instead of Snippets. The fact of the matter is that whenever a user selects "Delete" from the web page, the CGI program does not recognize the "Delete" parameter and removes all reservations from the page instead of just the one selected. I know this is a problem with the display_appointments function in addition to the fact that the key, when pressed, is not recognized. Basically, when its pressed, its a binary value. So as long as its not pressed all the appointments will display. But when you hit delete they all disappear from the web page but not the database because they are all of the same "submit form" instance. How do I go about making separate instances for each appointment and then checking to see which appointment has been chosen based on "delete instance"? I have also tried using an array to store the return value, but the same thing happens. Is there any way to say: if($cur->param("Delete") { # call delete appointments" } so that it checks each instance of delete to see which one was pressed?? The above statement doesn't work because "Delete" can't be set in the "action" part of the form."