WebmasterForums.NET - Webmaster Forums & Directory
 

Go Back   WebmasterForums.NET - Webmaster Forums & Directory > Design and Development > Database - Mysql, SQL and Others
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Database - Mysql, SQL and Others MySQL, MSSQL, PGSQL, ms access and other database discussions.

Reply
 
Thread Tools Display Modes
Old 07-31-2006, 01:08 PM   #1
Shoshana
Senior Member
 
Shoshana's Avatar
 
Join Date: Jul 2006
Location: NY
Posts: 104
Shoshana is on a distinguished road
Send a message via AIM to Shoshana
Default DBI MySQL problems

Hi, I've been having trouble with this script from day one. I'm not
sure what the problem is, but I'm trying to query the database with
the form parameters ie: $product_id = $q->param('product_id');. If the
item in question is already in the database, the script should UPDATE.
If not, the script should INSERT. Instead, I get a 500 error. If I
turn the warn flag on, I get:
"Filehandle main::header never opened at add.cgi line 29". and
"Use of uninitialized value at add.cgi line 107". I have totaly
revamped this script about four times, nothing seems to work. Any help
on this would be greatly apprecieated. Thanks in advance.
Cheers. Martinblack.
Below is a copy of the script:

#!/usr/bin/perl

BEGIN {
open (STDERR, ">html/error.txt");

}


### add.cgi
use DBI;
use CGI::Carp qw(fatalsToBrowser carpout);
use CGI;
$q=new CGI;

# input..................................

$io{'company_id'} = $company_id = $q->param('company_id');
$io{'product_id'} = $product_id = $q->param('product_id');
$io{'title'} = $title = $q->param('title');
$io{'description'} = $description = $q->param('description');
$io{'spex'} = $spex = $q->param('spex');
$io{'price'} = $price = $q->param('price');
$io{'sex'} = $sex = $q->param('sex');

# mainline...............................

print header;

$DBname= "dbi:mysql:minmin";
$DBhost = "localhost";
$DBusername = "minmin";
$DBpassword = "fFUZsFtD";

$dbh = DBI->connect("$DBname:$DBhost", $DBusername, $DBpassword ||
die "connection problem: ", $DBI::errstr;
$dbh->{'RaiseError'} = 1;

&kaboom;

&print_output || die "we've got a problem: ";

# Disconnect from the database
$dbh->disconnect || die "disconnection problem: ", $DBI::errstr;
exit;

# the subs...............................

sub print_output{
print<<HTML;
<HTML><BODY>
<CENTER><FONT SIZE=5>
Record added to database
<P>
he he he ha ha
</P>
</FONT></CENTER>
</BODY></HTML>
HTML

}


sub kaboom() {
$sth = $dbh->prepare( q[
SELECT company_id, product_id
FROM product
WHERE company_id = ? and
product_id= ?
] ) || die "prep problem: ", $DBI::errstr;
$sth->execute( $company_id, $product_id ) || die "execution problem:
", $DBI::errstr;

&fetch_results;

if ($newid) {

$dbh->do(do { local $" = ", "; qq[
UPDATE product
SET @{[map "$_ = ?", keys %io]}
WHERE company_id = ? AND product_id = ?
] }, undef,
values %io,
@io{qw(company_id product_id)} );

} else {

$dbh->do(do { local $" = ", "; qq[
INSERT INTO product (@{[ keys %io ]})
VALUES (@{[ ('?') x values %io ]})
] }, undef, values %io );

}

}


sub fetch_results {
while ($x = $sth->fetchrow_hashref) {
$com = $x->{'company_id'};
$pro = $x->{'product_id'};
}
$newid = $com ."-".$pro;
Shoshana is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 02:24 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0
WebmasterForums.NET - Webmaster Forums & Directory