Debug Help - Script to edit a flat file DB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deppeler
    New Member
    • Jul 2007
    • 40

    Debug Help - Script to edit a flat file DB

    Can someone look at this for me:
    I am trying to set up a script to edit an item in a flat file DB but I don't seem to be getting the data to the Photoedit script. It seems to be reading the 1st line of the DB and not what is selected.
    I am all messed up..I know this is straight forward but I am having a real hard time with this.

    I am wanting the user to be able to select a photo (nfile_name - radio button) and if desired alter the text cation (ncaption - text box)

    here is a portion of the code for the data I am collecting:

    [CODE=perl]
    #!/usr/bin/perl
    $dbp='/home/childcar/db/dbp.txt';
    $photopath='htt p://www.myorg.org/toypics/';
    print "Content-type: text/html\n\n";
    print <<"HTML";
    <head><title> </title/><body>
    <form action='photoed it.pl' method='GET'>
    <input type='hidden' name='nfile_nam e' value='$input{' nfile_name'}'>
    <input type='hidden' name='ncaption' value='$input{' ncaption'}'>
    <td bgcolor='#F5F5F 5' colspan='6'>
    HTML

    my $td_start = qq{<td bgcolor='#F5F5F 5' width='145' valign='top' align='center'> };
    my $td_start2 = qq{ border='0' width='25' height='25'><br ><INPUT TYPE='radio' NAME='nfile_nam e' value='};
    my $td_end = qq{</td>};
    my $showpic = q{<table cellpadding='2' border='0' width='729' cellspacing='2' bgcolor='#F5F5F 5'><tr>};
    open (BASE, $dbp) or no_open();
    my @sorted = sort(<BASE>);
    close BASE;
    my $i = 0;
    foreach my $pair (@sorted){
    $i++;
    my @show = split(/,/, $pair);
    $showpic .= qq{$td_start<in put type='text' name='ncaption' value='$show[1]' maxlength='10' size='15'><br>< img src='$photopath $show[0]'$td_start2$pho topath$show[0]'>$td_end};
    if ($i == 5) {
    $showpic .= '</tr><tr>';
    $i = 0; # <-- reset the flag
    }
    }
    $showpic .= '</tr></table>';
    print $showpic;
    print<<"HTML";
    </body>
    HTML
    [/CODE]

    here is the photoedit.pl script:

    [CODE=perl]
    #!/usr/bin/perl
    $database='/home/childcar/db/dbp.txt';
    $databaseview=' http://www.myorg.org/cgi-bin/admintoy/photo.pl';
    &parse_form;
    open (ORGDB,"<$datab ase");
    @ODB=<ORGDB>;
    close (ORGDB);
    open (DATABASE,">$da tabase");
    @DB=<DATABASE>;
    foreach $rec (@ODB){
    chomp($rec);
    ($file_name,$ca ption,)=split(/\,/,$rec);
    if ($file_name eq $input{'file_na me'} && $caption eq $input{'caption '}){
    print DATABASE "$input{'nfile_ name'},$input{' ncaption'},\n";
    }else{
    print DATABASE "$file_name,$ca ption,\n";
    }
    }
    close (DATABASE);

    print "Location: $databaseview\n \n";

    sub parse_form {

    read(STDIN, $buffer, $ENV{'CONTENT_L ENGTH'});
    if (length($buffer ) < 5) {
    $buffer = $ENV{QUERY_STRI NG};
    }
    @pairs = split(/&/, $buffer);
    foreach $pair (@pairs) {
    ($file_name, $value) = split(/=/, $pair);

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

    thanks
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    you have a hidden form field and a text form field both using the same name:

    name='ncaption'

    change or eliminate one of them. That might solve your problem.

    Comment

    • deppeler
      New Member
      • Jul 2007
      • 40

      #3
      I tried that but nothing.
      I did change the:
      Code:
      print "Location: $databaseview\n\n";
      to point back to the first script as I wanted to see any changes made.

      If I change this ($databaseview) to $file_name...$c aption I get a 'Not Found' error and the URL is the script with the Ist line of the database like this:

      Code:
      http://www.myorg.org/cgi-bin/admintoy/artsy.jpg,Art
      artsy.jpg is ($file_name) & ART is ($caption)


      Should I even have the GET method in that form or do I need another step between the first script and the edit script?

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        In the first block of code you posted above you have these lines:

        <input type='hidden' name='nfile_nam e' value='$input{' nfile_name'}'>
        <input type='hidden' name='ncaption' value='$input{' ncaption'}'>


        where is $input{'nfile_n ame'} and '$input{'ncapti on'} being defined?

        We also need to set some ground rules. Helping people with code that is not written using "stict" is something most people will not want to do, unless it is very short code. "strict" will insure you are defining and declaring your variables properly and other basic safe programming practices. Otherwise your scipt could be full of bugs and there is nothing worse than debuggingother peoples code first to make sure it's even correct. You should be using the CGI module to collect your form data. Here is your second block of code rewritten using strict and CGI:

        [CODE=perl]#!/usr/bin/perl
        use strict;
        use warnings;
        use CGI;
        my $q = CGI->new;
        my $database = '/home/childcar/db/dbp.txt';
        my $databaseview = 'http://www.myorg.org/cgi-bin/admintoy/photo.pl';

        my %input = $q->Vars;

        open (ORGDB,"<$datab ase") or die "Can't open $database for reading: $!";
        my @ODB = <ORGDB>;
        close ORGDB;

        open (DATABASE,">$da tabase") or die "Can't open $database for overwriting: $!";

        foreach my $rec (@ODB){
        chomp($rec);
        my ($file_name,$ca ption,)=split(/\,/,$rec);
        if ($file_name eq $input{'file_na me'} && $caption eq $input{'caption '}){
        print DATABASE "$input{'nfile_ name'},$input{' ncaption'},\n";
        }
        else{
        print DATABASE "$file_name,$ca ption,\n";
        }
        }
        close (DATABASE);
        print "Location: $databaseview\n \n";[/CODE]

        Right now your scripts are fairly simple so now is the time to implement "strict" and "warnings" before they grow into monsters and you have to kill them because they refuse to behave and you have no idea where the problem is.

        -Kevin

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          HERE!!! HERE!!! I second the motion that we set the ground rule and should become more strict about requiring code to have....well. "use strict;" and also "use warnings;".

          If I put up both hands does that count as two votes of support?

          Regards,

          Jeff

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Put your hands down. Your "two" votes have been recorded and.... errr.... properly filed. ;-)

            Comment

            Working...