>From a quick look at your code, here is the problem:
'$postInputs{'$fields[2]'}'

the single quotes will confuse your mysql

you might want to concider using the following syntax, which, in general, is
cleaner:

### something from a block of code i use ###
sub set_code { # usage setcode($string,$userid,$ip):
        use DBI;

        my $string = shift;
        my $owner = shift;
        my $ip = shift;

         my $sth = $dbh->prepare_cached('INSERT INTO code VALUES (_rowid, ?, ?,
?, NOW())')
                or die "Couldn't prepare statement: " . $dbh->errstr;
        my $rv = $sth->execute($string, $owner, $ip) or die "Couldn't execute
statement: " . $sth->errstr;
}

at the my $sth you see how we first prepare the query (i even use
prepare_cached here, which is helpful if you execute the same query multiple
times); for every variable, we put in a ? (_rowid is an autoincrement number,
and NOW() is a timestamp)
next, we execute the $sth with the variables we want interpolated
$rv will hold the return value of the query (generally the amount of rows
affected by your query)

see perldoc DBI for more info, or read the short guide to dbi on perl.com here:
http://www.perl.com/pub/1999/10/DBI.html

hope this helps,

Jos Boumans

ciberm3 wrote:

> Hi all,
>
> I have a problem using a hash variable and I don't know where is the error.
> Let me explain it: I have a web page with a form which user must fill in
> with personal data then when the user click 'Send' button the cgi I am
> programming starts; I have a txt file containing the name of the fields of
> the form which match with the name of the fields in the table I want to
> insert data so first thing I am doing in the cgi is obtaining data typed by
> the user in the form and I store it in a hash named %postInputs; then I use
> a function to initialize an array with the fields in the text file so the
> name of the array is @fields. No I am using next function to insert data in
> the table:
>
> ...
> %postInputs = readPostInput();
> readFields(@fields);
> ...
> insertData();
> ...
> sub insertData()
> {
>
>    my $base_datos = $fields[0];
>    my $driver = "mysql";
>    my $tabla = $fields[1];
>    my $sql_inserta="INSERT INTO $tabla (dia_alta,
>                                         mes_alta,
>                                         anno_alta,
>                                         $fields[2])
>                                   VALUES ('$mday',
>                                          '$mon',
>                                          '$year',
>                                          '$postInputs{'$fields[2]'}')"; #
> (*) HERE IS THE PROBLEM
>    my ($dbh) = DBI->connect("DBI:$driver:table_name") || die "\nError al
> abrir la base de datos: $dbh->errstr()\n";
>    $dbh->do($sql_inserta) || die "prepare: " . $dbh->errstr();
>    $dbh->disconnect || warn "\nError al desconectar.\nError:
> $DBI::errstr\n";
> }
>
> Then when I execute this function, data is inserted into the table except
> the field I mark (*). If I use ...,'nombre')" it works and insert this data
> into the table.
>
> On the other hand, in the DBI->connect sentence I have another problem
> because I must use DBI->connect("DBI:$driver:table_name") and I want to use
> DBI->connect("DBI:$driver:$base_datos"). I think the problem is the same.
>
> Please, does anyone know how can I solve this problem?.
>
> Many thanks for your help.
>
> [EMAIL PROTECTED]
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com

Reply via email to