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

hi. my $row[1], $row[2], and $row[3] variables get overwritten in the below program, once the user hits the "submit" button.. ive no idea why? please help.
#!/usr/bin/perl use DBI; use CGI qw/:standard *table start_ul/; $db_host = "localhost"; $db_user = "myuser"; $db_pass = "mypass"; my $dbh = DBI->connect("DBI:mysql:database=fnoofs;host=$db_host", $db_ +user, $db_pass, {'RaiseError' => 1}); opendir(DIR, "/web/htdocs/images/fnoofus") or die "wtf.. $!"; @allfiles = grep { $_ ne '.' and $_ ne '..' } readdir DIR; closedir DIR; print header, start_html('Fnoofus Admin Page'), start_form; $num = 0; print "begin foreach loop<br>"; foreach $file (@allfiles) { print "incrementing num<br>"; $num++; @checkname[$num] = $file . "_db"; $sth = $dbh->prepare("select * from fnoofus where picname='$file +'"); $number = $sth->execute(); print "<br>begin if loop<br>"; if ($number > 0) { print "begin while loop<br>"; while (@row = $sth->fetchrow_array) { print "<table>"; print "<tr><td rowspan=\"4\">"; print "<img src=\"/images/fnoofus/$file\"></td>"; print "<td valign=\"top\">"; print "Image location: ", textfield(-name=>'imageLoc',-defa +ult=>$row[1],-size=>50,-maxlength=>80),p; print "</td></tr><tr><td>"; print "Description: ", textfield(-name=>'description',-defa +ult=>$row[2],-size=>55),p; print "</td></tr><tr><td>"; print "Picname: ", textfield(-name=>'picname',-default=>$ro +w[3],-size=>20),p; print "</td></tr><tr><td valign=\"top\">"; print checkbox(-name=>$checkname[$num],-checked=>1,-value=> +'ON'); print "</td></tr>"; print "</table>", hr; } } else { print "<table>"; print "<tr><td rowspan=\"4\">"; print "<img src=\"/images/fnoofus/$file\"></td>"; print "<td valign=\"top\">"; print "Image location: ", textfield(-name=>'imageLoc',-defa +ult=>'nothing here',-size=>50,-maxlength=>80),p; print "</td></tr><tr><td>"; print "Description: ", textfield(-name=>'description',-defa +ult=>'nothing here',-size=>55),p; print "</td></tr><tr><td>"; print "Picname: ", textfield(-name=>'picname',-default=>'no +thing here',-size=>20),p; print "</td></tr><tr><td valign=\"top\">"; print checkbox(-name=>$checkname[$num],-checked=>0,-value=> +'OFF'); print "</td></tr>"; print "</table>", hr; } } print submit, end_form; if (param()) { foreach $check (@checkname) { if (param($check)) { print "$check<br>"; } } }

Replies are listed 'Best First'.
Re: variables get overwritten (loop problem? i dunno)
by gjb (Vicar) on Jan 04, 2003 at 04:37 UTC

    Okay, after staring at this for about half an hour I think I've got it. The error is in the HTML code you generate. Basically, you generate a table for each file found in a directory. This table contains some textfields, all of which are in the same form. Now all of the textfields for each of the files all have the same name, hence the data which is passed when the user hits the submit is just one of the fields with the same names, and that happens to be the first one.

    To solve this, generate a form per file, each containing the table you already create and each containing a submit button. That way only one textfield will be passed upon submit and you'll get the user's choice rather than the first in the form.

    Hope this helps, -gjb-

    Update: Depanding on what you want to do in the CGI script that receives this form's info, it may be quite dangerous to have the file names in the HTML form since it's quite easy to fake a response with other file names (/etc/passwd comes to mind ;-) Since the checkbox already indicates which file has been selected, it might be better to compute the file name and info from that information in the receiving CGI scipt.

      the only person to use this script (ever) is me. so i dont particularly care for the security of it.. ;) however. having a submit button per each picture is something im trying to avoid.. the purpose of this script is for me to easily add pictures to my webpage. the way im planning to do is by putting the new pictures in the directory, and have this script generate the form for me. that way ill easily see the filenames of the new pics, and can add description/imagelocation etc to my database. having to press 60+ submit buttons per once is tedious.. wanted to have one at the bottom. ;)

        Okay, I've made an attempt to make the code easier to understand by cleaning it up. Now have a look at the HTML that is generated by this code and you'll see how to get at the information for those images that have been selected using the checkboxes.

        Hope this helps, -gjb-

Re: variables get overwritten (loop problem? i dunno)
by Anonymous Monk on Jan 04, 2003 at 04:10 UTC
    Use strict and use warnings. I also didn't enjoy searching for those variables in that sea of print statements. I could just see Randal cringing. Try using CGI.pm html short cuts. Then come back and ask again. . . . ;-)
Re: variables get overwritten (loop problem? i dunno)
by Anonymous Monk on Jan 04, 2003 at 04:17 UTC
    You may find Why Questions go Unanswered useful. Just the act of getting rid of extraneous code that doesn't illustrate what puzzles you often will answer your question.

    BTW I am curious about what you expected to happen when you assigned to @row. I would expect to replace the array each time. You?