Maxipoint Rep Office <[EMAIL PROTECTED]> wrote:
: 
: Charles [mailto:[EMAIL PROTECTED] wrote:
: : 
: : Maxipoint Rep Office <[EMAIL PROTECTED]> wrote:
: : 
: :
: : : my $turistobjekt = "dadas";
: : : my $adresa = "sdasdasdfADRESA";
: : : my $brojgrada = "1";
: : : my $grad = "1";
: : : my $manjemjesto = "1";
: : : my $otok = "1";
: : : my $regija = "1";
: : : my $ime = "1";
: : : my $prezime = "1";
: : : my $tel = "1";
: : : my $mob = "1";
: : : my $fax = "1";
: : : my $email = "1";
: : : my $username = "1";
: : : my $password = "1";
: : : my $status = "1";
: : 
: :     Are all these variables supposed to be coming from
: : an HTML form? Is that what you want to use CGI.pm for?
:
: RE: yes, I wish delete it in script and that form send same
: values using CGI.pm and HTMP::template

[snip]

: : : $dbh->do("INSERT INTO portal (turistobjekt, adresa, brojgrada,
: : : grad, manjemjesto, otok, regija, ime, prezime, tel, mob, fax,
: : : email, username, password, status) VALUES ('$turistobjekt',
: : : '$adresa', '$brojgrada', '$grad', '$manjemjesto', '$otok',
: : : '$regija', '$ime', '$prezime', '$tel', '$mob', '$fax',
: : : '$email', '$username', '$password', '$status')" or die
: : : $dbh->errstr); 

     Let's change this to something a bit more readable. Like
this we can see the order needed and a shortcut to building
the INSERT.

    INSERT INTO portal (
        turistobjekt,
        adresa,
        brojgrada,
        grad,
        manjemjesto,
        otok,
        regija,
        ime,
        prezime,
        tel,
        mob,
        fax,
        email,
        username,
        password,
        status
    )
    VALUES (
        '$turistobjekt',
        '$adresa',
        '$brojgrada',
        '$grad',
        '$manjemjesto',
        '$otok',
        '$regija',
        '$ime',
        '$prezime',
        '$tel',
        '$mob',
        '$fax',
        '$email',
        '$username',
        '$password',
        '$status'
    )


    I'm not a DB expert, but the module does mention that
INSERTs should be of the this form. The question (?) marks are
place holders.

INSERT INTO sales (id, qty, price) VALUES (?, ?, ?)

    The module also shows the form of a do() with place
holders. We'll model our do() on the same form. Since we don't
have %attr we will use 'undef'.
    
$dbh->do($statement, \%attr, @bind_values);


    We need to end up with this. But we need to be certain we
can easily adjust our fields in the future.
    
    INSERT INTO portal (
        turistobjekt,
        adresa,
        brojgrada,
        grad,
        manjemjesto,
        otok,
        regija,
        ime,
        prezime,
        tel,
        mob,
        fax,
        email,
        username,
        password,
        status
    )
    VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )


    To do that we need only know the order and names of those
fields. An array would probably be best.

my @ordered_fields = qw(
    turistobjekt    adresa  brojgrada   grad    manjemjesto
    otok            regija  ime         prezime tel
    mob             fax     email       username
    password        status
);

    We will need the Portal fields.

my $portal =
    'portal (' .
    join( ',', @ordered_fields ) .
    ')';

    And the place holders.

my $place_holders =
    '(' .
    join( ',', ('?') x @ordered_fields ) .
    ')';

    An INSERT.
    
my $insert = qq(INSERT INTO $portal VALUES $place_holders);
    
    Finally we need an ordered list of fields from the form.

my $q       = CGI->new();
my %params  = $q->Vars;

@params{ @ordered_fields }

    Now, we put it all together. (This is not tested.)

my $q       = CGI->new();
my %params  = $q->Vars();

my @ordered_fields = qw(
    turistobjekt    adresa  brojgrada   grad    manjemjesto
    otok            regija  ime         prezime tel
    mob             fax     email       username
    password        status
);

my $portal =
    'portal (' .
    join( ',', @ordered_fields ) .
    ')';

my $place_holders =
    '(' .
    join( ',', ('?') x @ordered_fields ) .
    ')';

my $insert = qq(INSERT INTO $portal VALUES $place_holders);

my $return_value =
    $dbh->do( $insert, undef, @params{ @ordered_fields } );


    Should we need to change the order or names of fields
in the future, we need only change the form and
@ordered_fields.



HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to