Advertisement

04.04.2008 at 04:59AM PDT, ID: 23295688
[x]
Attachment Details

Database Entry

Asked by narmi2 in Perl Programming Language, CGI Scripting

Tags: Perl

Dear Experts,

The following script does not work and I cannot see why.  Can you please help?

The error message I get is:

[Fri Apr 04 12:50:31 2008] [error] [client 000.000.000.00] "my" variable $sql masks earlier declaration in same scope at /var/www/html/www.mywebsite.com/site/cgi-bin/test.cgi line 45.
[Fri Apr 04 12:50:31 2008] [error] [client 000.000.000.00] DBD::mysql::st execute failed: You have an error in your SQL syntax near 'Test' at line 1 at /var/www/html/www.mywebsite.com/site/cgi-bin/test.cgi line 13.
[Fri Apr 04 12:50:32 2008] [error] [client 000.000.000.00] Couldn't open encmap windows-1252.enc:
[Fri Apr 04 12:50:32 2008] [error] [client 000.000.000.00] No such file or directory
[Fri Apr 04 12:50:32 2008] [error] [client 000.000.000.00]  at /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/XML/Parser.pm line 185
[Fri Apr 04 12:50:32 2008] [error] [client 000.000.000.00] Premature end of script headers: test.cgiStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
#!/usr/bin/perl -w
use XML::Twig;
use DBI;
 
my $dsn = 'DBI:mysql:database_name:localhost';
 
my $user = 'user';
my $pass = 'pass';
 
my $dbh = DBI->connect($dsn, $user, $pass) or die "Can not connect to the DB: $DBI::errstr\n"; 
 
$cur = $dbh->prepare('Test');
$cur->execute();
 
my $file = 'http://feeds.externalcompany.co.uk/?fc154d69-0c4d-42b8-93a4-4a20cg5225e1';
my $twig = XML::Twig->new();
 
$twig->parseurl($file);
 
my $root = $twig->root;
 
foreach my $article ($root->children('Article'))
{
   my $itemid = $article->att('ID');
   my $heading = $article->first_child_text('Heading');
   
   #replacing single quote with '' in the heading tag to avoid problems with inserting into MySQL
   $heading =~ s/'/''/g;
   my $contents = $article->first_child_text('Contents');
   
   #replacing single quote with '' in the Contents tag to avoid problems with inserting into MySQL
   $contents =~ s/'/''/g;
   my $articledate = $article->first_child_text('Date');
   
   #Putting the date into the correct format for MySQL database
   @splitdate = split(/\//, $articledate);
   $date = $splitdate[2]."-".$splitdate[1]."-".$splitdate[0];
   
   # delete the item from the database if it exists
   my $sql = qq{ DELETE FROM Test WHERE ItemId=$itemid };
   $dbh->do( $sql );
      
   #insert the item into the database
   my $sql = qq{ INSERT INTO Test (ItemID, Heading, Contents, Date) VALUES ($itemid, '$heading', '$contents', '$date') };
   $dbh->do( $sql );
}
 
$cur->finish();
exit 0;
[+][-]04.04.2008 at 05:46AM PDT, ID: 21281033

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Perl Programming Language, CGI Scripting
Tags: Perl
Sign Up Now!
Solution Provided By: kawas
Participating Experts: 1
Solution Grade: A
 
 
[+][-]04.04.2008 at 07:35AM PDT, ID: 21282171

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.04.2008 at 07:39AM PDT, ID: 21282203

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.04.2008 at 07:39AM PDT, ID: 21282209

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.04.2008 at 08:02AM PDT, ID: 21282476

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628