This question belongs in dbi-users, not dbi-dev. Unless you are offering to
supply a patch to DBI or a DBD.
Comments interspersed.
--
Mac :})
** I normally forward private database questions to the DBI mail lists. **
Give a hobbit a fish and he'll eat fish for a day.
Give a hobbit a ring and he'll eat fish for an age.
----- Original Message -----
From: "Ray Cuzzart II" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, May 15, 2001 22:49
Subject: INSERT INTO $tbl VALUES @array Help!!!!
> What I am trying to do is insert form fields into a mySQL database
> table by using them in an array. I have the array setup properly it
collects
> all the fields but when I try to insert it into the table I get and error.
> Below
> is the information on what I am running as well as the code snippet and
> error
> message. Any help would be greatly appreciated. The reason for using an
> array
> is because there is 32 fields and I would rather use it this way then to
say
> my $field = param('field'); for 32 lines.
>
> Environment Information:
> mySQL Version: 3.23.33
> Server Apache Version: 1.3.19
> OS: Windows
>
> DBI Version: Most current
> DBD::mysql version: most current
>
> Script:
>
> Top:
> use strict;
>
> use CGI::Carp "fatalsToBrowser";
> use CGI ":all";
If you are using the OO interface, you shouldn't need to import much
> use DBI;
>
> my $servname = "localhost";
> my $servuser = "inventory";
> my $servpass = "inventory";
> my $db = "inventory";
> my $tbl = "vehicles";
>
> my ($dbh);
>
> $dbh = DBI->connect("DBI:mysql:$db",$servuser,$servpass) or die "Unable to
> connect to inventory database: $dbh->errstr\n";
$dbh->errstr is a method call and won't interpolate correctly in a string.
Use $DBI::errstr instead.
> my $obj = new CGI;
> my @values = map { $obj->param($_) } $obj->param();
> my @all = join(",", @values);
>
> foreach (@all) {s/addvehicle,//;}
> my $all = "@all";
This should probably report an error, check your webserver log. In any
case, it is probably not doing what you think it is. I'd use <my $ph = join
",", map { '?' } @all;> to build a list of placeholders to use in the SQL.
> my $success = $dbh->do("INSERT INTO $tbl VALUES $all") or die "Unable to
> write information to inventory database: $dbh->errstr\n";
I'd replace the SQL with "INSERT INTO $tbl (<collist>) VALUES ($ph)". If
the table definition changes in almost any way, the INSERT without a column
list will break. With placeholders, your do() can be:
my $success = $dbh -> do( "INSERT INTO $tbl ( <collist> ) VALUES ($ph)",
@all ) or die "Insert failed, $DBI::errstr";
> $dbh->disconnect;
>
> if($success != 1) {
> print header, start_html("Entry Added");
> print "Sorry, the database was unable to add your entry. Please try again
> later.";
> } else {
> print header, start_html("Entry Added");
> print "Information Added\n";
> }
>
> That is what I am using I get an error at the my $success =
$dbh->do("INSERT
> INTO $tbl VALUES $all") or die "Unable to write information to inventory
> database: $dbh->errstr\n"; process.
>
> Error Message:
>
> Unable to write information to inventory database:
> DBI::db=HASH(0x1a94a1c)->errstr