Hi all, i 've been looking for better way to do this so here it is...

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 ...


In reply to DBI conditional insert (and things of those nature) by data67

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.