Link to home
Start Free TrialLog in
Avatar of g_currier
g_currierFlag for Germany

asked on

perl uninitialized error

Hi,

I am trying to draw the last created record's id number from an access database and use it in perl to repopulate a new record (with the drawn id number +1).
I keep getting an "uninitialized value...in addition (+) at..." error.

I'm still kind of fresh to perl. Can someone take a peek at my code and tell me what's wrong?

Thanks.
#!c:\perl\bin\perl.exe -w

use strict;
use CGI ':standard';
use Win32::ODBC; #use ODBC package

### Connect to a data source
my $db = new Win32::ODBC("DSN=rreAccesscm485a5") || die Win32::ODBC::Error();

### Perform INSERT of new customer

#Retrieve values for new customer from HTML form

my $f_name	= param('f_name');
my $mi		= param('mi');
my $l_name	= param('l_name');
my $h_phone	= param('h_phone');
my $street	= param('street');
my $city	= param('city');
my $state	= param('state');
my $zip		= param('zip');

#Getting a value for the next customer ID
my $last_id;

$db-> FetchRow;
$last_id = $db->Data();
my $next_id = $last_id+1;

my $sql;

$sql = qq{INSERT INTO customer (cust_id, f_name, mi, l_name, h_phone, address_street, address_city, address_state, address_zipcode)};
#$sql .= qq{VALUES ('$next_id','$f_name', '$mi', '$l_name', '$h_phone', '$street', '$city', '$state', '$zip' ) };
print 'SQL string to execute=' . $sql . "\n";

#execute INSERT statement
$db->Sql($sql); 
print "<h2>Database INSERT successful.</h2>";
# Disconnect
$db->Close();
# Provide an option to show updated table contents
print '<form ACTION="cust_data.pl" METHOD="get">' .
'<input TYPE="submit" VALUE="Show All Customers">' .
'</form>'

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of g_currier

ASKER

yes, $last_id is undefined.  But what do i define it as except for $last_id = $db->Data(); ? It was my thinking that that statement was what defined the variable.  Obviously I am wrong, but how?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is what I, in the end, came up with...it's not perfect (the sql doesn't work - it seems as if the param() function is doing it's job).

The query is written to select the cust_id value (no I didn't write it, but I understand the sql syntax int it)
The $next_id coding is working...a value is generated.

granted, I need to read more and understand better what Data() , param() do and what fetchrow does.

I thought I understood SQL, however.  the insert statement si something that perl doesn't like (at least, i think, the way I wrote it...)
#!c:\perl\bin\perl.exe -w

use strict;
use CGI ':standard';
use Win32::ODBC; #use ODBC package

### Connect to a data source
my $db = new Win32::ODBC("DSN=rreAccesscm485a5") || die Win32::ODBC::Error();

### Perform INSERT of new customer

#Retrieve values for new customer from HTML form



#Getting a value for the next customer ID
#Get last Customer ID used
if ($db->Sql("SELECT MAX(cust_id) as last_cust_id FROM customer")) {
print "SQL Error: " . $db->Error() . "\n";
$db->Close();
exit;
}

#Determine next Cust ID based on existing data
my $last_cust_id;
my $next_cust_id;
my $cust_id;
$db->FetchRow;
$last_cust_id = $db->Data();
$next_cust_id = $last_cust_id + 1;

my $f_name;
$f_name		= param('f_name');
# DEBUG
print "First Name: " . $f_name;
my $mi_name;
$mi_name	= param('mi_name');
my $l_name;
$l_name		= param('l_name');
my $h_phone;
$h_phone	= param('h_phone');
my $address_street;
$address_street		= param('address_street');
my $address_city;
$address_city		= param('address_city');
my $address_state;
$address_state		= param('address_state');
my $address_zipcode;
$address_zipcode	= param('address_zipcode');
my $sql;

$sql = qq{INSERT INTO customer (cust_id, f_name, mi_name, l_name, h_phone, address_street, address_city, address_state, address_zipcode)VALUES ("$next_cust_id","$f_name", "$mi_name", "$l_name", "$h_phone", "$address_street", "$address_city", "$address_state", "$address_zipcode" ) };
#$sql .= qq{};
print 'SQL string to execute=' . $sql . "\n";

#execute INSERT statement
$db->Sql($sql); 
print "<h2>Database INSERT successful.</h2>";
# Disconnect
$db->Close();
# Provide an option to show updated table contents
print '<form ACTION="cust_data.pl" METHOD="get">' .
'<input TYPE="submit" VALUE="Show All Customers">' .
'</form>'

Open in new window

Problem solved on my own.  This problem had to do with the html <form> tag.  The closing form tag was odid not include the table into it.  Therefore, the param function:  my $action = param('addrecord') was not working.  I added that variable after I reread through my perl handbook.  crap this gets complicated if you're not paying attention!

Thanks for the tips all!
The solution was found by me, but the tips provided helped me to solve other related problems