Solved

perl uninitialized error

Posted on 2010-09-09
9
378 Views
Last Modified: 2013-12-25
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

0
Comment
Question by:g_currier
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
Comment Utility
It might help if you showed what was in the  ...
splain
/usr/bin/splain: Reading from STDIN
Use of uninitialized value $a in addition (+) at -e line 1.
Use of uninitialized value $a in addition (+) at -e line 1 (#1)
    (W uninitialized) An undefined value was used as if it were already
    defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
    To suppress this warning assign a defined value to your variables.
   
    To help you figure out what was undefined, perl will try to tell you the
    name of the variable (if any) that was undefined. In some cases it cannot
    do this, so it also tells you what operation you used the undefined value
    in.  Note, however, that perl optimizes your program and the operation
    displayed in the warning may not necessarily appear literally in your
    program.  For example, "that $foo" is usually optimized into "that "
    . $foo, and the warning will refer to the concatenation (.) operator,
    even though there is no . in your program.


the only + I see in your program is
my $next_id = $last_id+1;
which could suggest that $last_id was undefined
0
 

Author Comment

by:g_currier
Comment Utility
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?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
Comment Utility
Did FetchRow find anything? you did not check whether it succeeded.
I also do not see a query being made


FetchRow ( [ ROW [, TYPE ] ] )

    Retrieves the next record from the keyset. When ROW and/or TYPE are specified, the call is made using SQLExtendedFetch instead of SQLFetch.

    NOTE 1: If you are unaware of SQLExtendedFetch and its implications, stay with just regular FetchRow with no parameters.

    NOTE 2: The ODBC API explicitly warns against mixing calls to SQLFetch and SQLExtendedFetch; use one or the other but not both.

    If ROW is specified, it moves the keyset RELATIVE ROW number of rows.

    If ROW is specified and TYPE is not, then the type used is RELATIVE.

    Returns true when another record is available to read, and false when there are no more records
0
 
LVL 5

Assisted Solution

by:fredmc
fredmc earned 50 total points
Comment Utility
What ozo is implying, but I don't see her explicitly saying, is that if $db->Data(); returns no value, then $last_id will be unitialized. You might try this (including all that ozo suggested)
my $last_id = '';  
to give it an inital value of an empty string. Then,
 $last_id = $db->Data() || '';
which will give $last_id a value of an empty string if $db->Data() returns nothing. An empty string isn't the same as "nothing" or undefined.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 25 total points
Comment Utility
I think what ozo was saying is that:
    $db-> FetchRow;
occurs before any query is define.  So there is no row to fetch.  
0
 
LVL 5

Assisted Solution

by:fredmc
fredmc earned 50 total points
Comment Utility
Correct. Which is why I said "including all that ozo suggested"
:-)
0
 

Author Comment

by:g_currier
Comment Utility
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

0
 

Author Comment

by:g_currier
Comment Utility
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!
0
 

Author Closing Comment

by:g_currier
Comment Utility
The solution was found by me, but the tips provided helped me to solve other related problems
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now