On Fri, Oct 12, 2001 at 01:24:36PM +0800, barry kwok wrote:
> if I insert record by perl script with statement like this:
> my $sth = $dbh->prepare("insert into mytest1 values (\"011011\",\"test\")") or die
>"Can't prepare statement: ".$dbh->errstr;
> the record can be inserted. But if I use variable
> $data1="011011";
> $data2="test";
> and insert as
> my $sth = $dbh->prepare("insert into mytest1 values ($data1,$data2)") or die "Can't
>prepare statement: ".$dbh->errstr;
> I got this error:
> DBD::mysql::st execute failed: Unknown column 'test' in 'field list' at ./select
> 1.pl line 7.
> I have done numerous tests and found that if I change the $data2 to a numeric, it
>can inserted.
>
The error message is perfectly accurate. The SQL statement
insert into mytest1 values (011011,test)
does not work because there is no column named test. Literal strings in
SQL need to be quoted. Literal numbers do not.
Use placeholders or $dbh->quote().
Ronald