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

$ins = "INSERT INTO `items` (`category`, `itemid`, `price`, `descripti +on`, `longdescription`, `size`, `o1n`, `o1o`, `o2n`, `o2o`, `o3n`, `o +3o`, `c1n`, `c1v`, `c2n`, `c2v`, `c3n`, `c3v`, `small`, `large`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, +?, ?)"; $sth = $dbh->prepare("$ins") or die $dbh->errstr; $sth->execute(param('cat'),param('itemid'),param('price'),param('des') +,param('longdes'),param('size'),param('o1n'),param('o1o'),param('o2n' +),param('o2o'),param('o3n'),param('o3o'),param('c1n'),param('c1v'),pa +ram('c2n'),param('c2v'),param('c3n'),param('c3v'),param('small'),para +m('large')) or die $dbh->errstr;
Gettting this error, dont understand why, cause the each have 20.
called with 19 bind variables when 20 are needed at /home/dtdynoco/pub +lic_html/admin1130/admin.cgi line 1023.

Replies are listed 'Best First'.
•Re: called with 19 bind variables when 20 are needed, error
by merlyn (Sage) on Jul 20, 2002 at 21:41 UTC
    param in a list context (as you have it) can return 0 or many parameters. (We've hit this subject here before if you search a bit.)

    If you add scalar in front of each param, or do something like this instead, you'll be happier:

    $sth->execute(map scalar param($_), qw(cat itemid price des longdes si +ze o1n o1o o2n o2o o3n o3o c1n c1v c2n c2v c3n c3v small large)) or d +ie $dbh->errstr;
    In fact, there's still far too much regularity in that code for my taste. You should abstract out the fields and mapping from form names to db columns.

    -- Randal L. Schwartz, Perl hacker

      $sth->execute(map scalar param($_), qw(cat itemid price des longdes size o1n o1o o2n o2o o3n o3o c1n c1v c2n c2v c3n c3v),$small,$large) or die $dbh->errstr; Why doesnt that work
        Because $small and $large are being passed through the map. You need to add parens to the map arg list:
        $sth->execute(map(scalar param($_), qw(cat itemid price des longdes si +ze o1n o1o o2n o2o o3n o3o c1n c1v c2n c2v c3n c3v)),$small,$large) o +r die $dbh->errstr;

        -- Randal L. Schwartz, Perl hacker

Re: called with 19 bind variables when 20 are needed
by gav^ (Curate) on Jul 20, 2002 at 22:42 UTC
    Personally I find something like this easier to read and definatly less error prone, the field names are only in one place:
    my @fields = qw/cat itemid price des longdes size o1n o1o o2n o2o o3n o3o c1n c1v c2n c2v c3n c3v small large/; my $sql = sprintf 'INSERT INTO items (%s) VALUES (%s)', join(',', @fields), join(',', (?) x @fields); $dbh->do($sql, undef, map scalar param($_), @fields);

    gav^