PDA

View Full Version : Adding new record using dbi


tom123
08-08-2006, 04:46 PM
Hey guys,

Having real trouble with code below. It wont add the record to the database, any suggestions?

member, non-member are number types in db
date_start & date_end are Date types in db.


my ($db,$ref) = @_;
my %products = %{$ref};

# Create query to add new product
my $query = qq(INSERT INTO t_mou_eco_product
VALUES ($products{'code'},
$products{'name'},
$products{'description'},
NULL,
$products{'currency'},
'$products{'non_member_price'}',
'$products('member_price'}',
NULL,
$products{'date_start'},
$products{'date_end'},
1,
NULL,
NULL,
NULL,
NULL,
$products{'awards_type'}));

# Run query and create handle for it
$db->Execute($query);
#print($query);
$db->Commit();

Thanks in advance

KevinADC
08-08-2006, 05:36 PM
I don't use the DBI module much but I am pretty sure all the sub-routine names are all lower-case.

Execute and Commit probably should be written as:

$db->execute()
$db->commit()

of course I am assuming everything else about your script is correct and changing the names to all lower case will solve the problem.

FishMonger
08-08-2006, 06:23 PM
In addition to the case issue that Kevin points out, you also have a quoting issue.

Execpt for the NULL's, you should quote each of the values like you did with '$products('member_price'}'. Numerical values can sometimes be left unquoted, but I've had intermittent problems with my inserts when they were unquoted.

tantric
08-08-2006, 09:26 PM
always use $foo=$dbh->quote($foo)

this also helps:

%dBattr = (
PrintError => 0,
RaiseError => 0
);

$dbh = DBI->connect($server, $ID, $pword, \%dBattr) or die("didn't connect", $DBI::errstr);

$sth=$dbh->prepare($insertstatement);
$sth->execute or print $sth->errstr());

tom123
08-09-2006, 10:52 PM
Thanks for all the help guys
:thumbsup: