in reply to Re: Database issues
in thread Database issues

Snippet:
# have a db and hash $stats open # form is incomplete in the snippet but you get the general idea # print "Name: " textfield( -name => 'name', -size => 100); print "age: " textfield( -name => 'name', -size => 5); my $name = param('name'); my $age = param('age'); #store every person from the form into the database $stats{$name} = $age;

Replies are listed 'Best First'.
Re: Re: Re: Database issues
by derby (Abbot) on Apr 09, 2003 at 16:33 UTC
    ... print "age: " textfield( -name => 'age', -size => 5); ...

    -derby

Re: Re: Re: Database issues
by nothingmuch (Priest) on Apr 09, 2003 at 16:42 UTC
    Regardless of what you print beside it the form item has a value called name. You set them both to be 'name'. When you submit the form with

    Name: [ george ] Age: [ 6 ]


    when using a GET type request, note that the cgi will be appended with ?name=george&name=6.

    This describes the parameters, and in fact, the outcome is that there is no parameter named 'age'. your fix is this:
    print "age: " textfield ( -name => 'age', -size => 5);


    -nuffin
    zz zZ Z Z #!perl
      Ok, I am getting so frustrated now. I removed the scalar as a key and just put 'name' as a test ($stats{'name'} = $name) but even THAT only stores on value at a time! Can someone tell me why?
        This should do what you mean:
        print "Name: " textfield( # this field contains the name (e.g. 'georg +e','bill', whatever) -name => 'name', -size => 100); print "age: " textfield( # this is the age of whoever's name is in t +he field above (possibly) -name => 'age', -size => 5); my $name = param('name'); # the name we got back is placed in the vari +able $name my $age = param('age'); # the age we got back is placed in $age $stats{$name} = $age; # lets associate $name with $age in a hash


        -nuffin
        zz zZ Z Z #!perl
      I did your latest suggestion putting it back as a scalar but it DOESN'T change anything! Whether i use 'email' or $email (the latter should be dynamic so they can't over write eachother) I get the same result.
        so you actually want the following to happen?
        %hash = ( 'george@foo.com' => 'george ); # how it is when george is ad +ded %hash = ('bill@host.com' => 'bill'); # bill clobbers george
        You can do this much more simply by using Storable or Data::Dumper without using a hash. Hashes are ment to store large amounts of data. You are more likely to find an array more suitable ([ 'george@foo.com', 'george' ]), or using your hash to describe who george is:
        %hash = ( email => 'george@foo.com', name => 'geroge fubar', age => 68, );
        Which you can acheive by writing to hash entries like your params:
        my $email = params('email'); my $name = params('name'); $hash{email} = $email; $hash{name} = $name;


        -nuffin
        zz zZ Z Z #!perl
      This same code does the exact same as using a scalar as the key, it only saves one thing. This is using the method $hash{this} = $that now and still it's screwing something up. rrrr, this isn't fun anymore!
      print start_form(), table( Tr( td("Name: "), td( textfield( -name => 'name', -size => 40 ) ) ), Tr( td("Email: "), td( textfield( -name => 'email', -size => 40 ) ) ), Tr( td(), td(submit) ), ), end_form(), hr(); if ( param() ) { my $email = param('email'); my $name = param('name'); if ( param('email') ne "" ) { tie %this, 'SDBM_File', $list, O_CREAT | O_RDWR, 0644; if ( !tied %this ) { warn("database unsuccessful $!.\n"); } ################# Next line ethis{'email'} = "$name"; }
        This is the structure of the hash you want:
        %hash = ( # george is added 'george@host.com' => 'george', ); %hash = ( # bill is added 'george@host.com' => 'george', 'bill@company.net' => 'bill', );
        But you're not setting with the actual value of the email field that has been passed. You're setting the key to the value 'email', instead of an email address, thus producing
        %hash = ( # george is added 'email' => 'george', ); %hash = ( # bill is added 'email' => 'bill', );
        The outcome is that they're names are overwritten, because you're accessing the field by the same key, which is 'email', instead of $email, they're addresses.

        Your fix is:
        $this{$email} = $name;


        -nuffin
        zz zZ Z Z #!perl