data67 has asked for the wisdom of the Perl Monks concerning the following question:
BACKGROUND
I have this program here that inserts a new employee to my Oracle table. As you can see below i am filling my variables
with parameters passed from a different program and using a "filtering" sub that i found on line to take out the (')s.
And all of this is forking fine but...
PROBLEM
Here's whats wrong:
There are two concerns that i have, but you may find more that need attention.:
QUESTION_ONE
Before i insert any record i need to make sure that an employee with that particular "employee_number" does not exist.
And if it does exist, print a message on the browser that it exists. If not go ahead and insert and print confirmation.
what would be the best way? should i use SQL like selec count(*) or use the DBI $sth->rows. A good conditional insert example would definately help.
QUESTION_TWO
Look at the sub called "filter", this is something that i saw a guy use on-line. But, DBI has a thing called $sql = $dbh->quote($string);
that can take care of unwanted charachters in your program (e.g, ", ' ) but i saw this used for the SQL rather than to take care of variables. By the way, if i end up using the filter subroutine, it need to take care of "(double-quotes) also.
Is there a way i can use DBI-quote() to handel problem characters or should i just modify the filter subroutine to check for (") also.
Here is what i have so far
use DBI; $DSN = "dbi:Oracle:host=localhost;sid=essinv;port=1521"; $user = "username"; $pw = "password"; $dbh = DBI->connect($DSN,$user,$pw, { RaiseError => 1, AutoCommit => +1}) || die "Cannot connect: $DBI::errstr\n" unless $dbh; # get params & set variables my $employee_number = &filter(param('employee_number')) +; my $last_name = &filter(param('last_name')); my $first_name = &filter(param('first_name')); print header (); eval{ $SQL = "INSERT INTO EMPLOYEE ( EMPLOYEE_NUMBER, FIRST_NAME, LAST_NAME, INFORMATION_DATE +) VALUES ('$employee_number', '$last_name', '$first_name', + SYSDATE)"; $sth = $dbh->prepare($SQL); }; # End of eval # Check for errors. if($@){ $dbh->disconnect; print "Content-type: text/html\n\n"; print "An ERROR occurred! $@\n<P>"; exit; } else { $sth->execute; } # End of if..else ## Filter - Gets rid of characters that screw up the program. sub filter{ $_[0]=~s/\'/\\\'/g; return $_[0]; } # End of filter subroutine # Disconnect from the database $sth->finish; $dbh->disconnect;
Thanks ...
|
|---|