I currently have a reporting cgi that takes html form output to perl cgi scripts that store the data into a flat file and e-mails the notification to distrobution lists.
I have been asked to expand this to not only cover my department but also some others in the company. To do this I need to import the data into a MySQL database. I wrote a short insert script that works for many of the entries, but any entry that contains cerain characters, like the ', it will errror out.
I have condensed the script to the offending section and included a CLI output for your parsing. Of course, db and server information has been changed to protect my company and myself.
Any assistance will be greatly appreciated.
###########################################################
#!/usr/bin/perl -w
use DBI;
my ($st) = 0;
if ( &db_connect() ) {
exit (1);
}
print "Please insert some text: ";
$sometext = <STDIN>;
chomp ($sometext);
$sql = qq { insert into my_table (update_1) values ('$sometext') };
$sth = $dbh->prepare($sql);
$sth->execute();
if ( $DBI::err ) {
if ( $DBI::err == 1062) {
print "\nDatabase server error: $DBI::err \n";
print "Record already exists in database. \n";
$st++;
}
else {
print "Database server error: $DBI::err \n";
$st++;
}
}
$dbh->disconnect();
sub db_connect {
my $user = "user";
my $pass = 'password';
my ($st) = 0;
$dbh = DBI->connect("dbi:mysql:host=my.work.domain", $user, $pass)
+ or die "Database Connection not made: $DBI::errstr \n";
if ( ! $dbh ) {
print "Error opening database: \n $DBI::err \n $DBI::errstr \n
+"; $st++;
}
$dbh->do("use mydb");
return ($st);
}
############################################################
root@myserver scripts# perl migrate_data_short.pl
Please insert some text: An alarm was triggered for an OC-48 between Seattle (cr1.sea20) and Pullman, WA (PLMNWA-rtr2). Pullman shows having a redundant link out through Moscow, to Coeur d'Alane which then connects to Seattle (cr0.sea20). Heath at NTI was contacted to investigate the circuit.
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Alane which then connects to Seattle (cr0.sea20). Heath at NTI was contacted to ' at line 1 at migrate_data_short.pl line 14, <STDIN> line 1.
Database server error: 1064
root@myserver scripts#
If I omit the ' from "Coeur d'Alane" it posts just fine. How do I get chacters like this to be inserted to my db/table?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.