#!/usr/bin/perl use strict; use warnings; use CGI; use DBI; use XML::Twig; use CGI::Carp 'fatalsToBrowser'; my $cgi = CGI->new; print $cgi->header(-type => "text/xml", -charset => "utf-8"); my $xml = $cgi->param("POSTDATA"); print "the data $xml is received"; my $username = "abcxyz"; my $password = "xyzabc"; my $dsn = "dbi:mysql:twig:127.0.0.1"; my $dbh = DBI->connect($dsn,$username,$password) or die "Cannot connect to database: $DBI::errstr"; my $sql1 = 'INSERT INTO twig_test(name,ppg,rpg,apg,g,blk) VALUES(?,?,?,?,?,?)'; my $sth1 = $dbh->prepare($sql1); my $sql2 = 'INSERT INTO customer(first_name,last_name,dob,email) VALUES(?,?,?,?)'; my $sth2 = $dbh->prepare($sql2); my $sql3 = 'INSERT INTO supplier(name,address,tel_no) VALUES(?,?,?)'; my $sth3 = $dbh->prepare($sql3); #test if($dbh){ print "print successfully connected to the database"; } my $twig = new XML::Twig( twig_handlers => {player => \&player, customer => \&customer, supplier => \&supplier} ); $twig->parsefile($xml); #$twig->print; sub player {my ($twig,$player) = @_; my @f=(); $f[0] = $player->field('name'); $f[1] = $player->field('ppg'); $f[2] = $player->field('rpg'); $f[3] = $player->field('apg'); $f[4] = $player->field('g'); $f[5] = $player->field('blk'); print $f[0], "\n"; print $f[1], "\n"; print $f[2], "\n"; print $f[3], "\n"; print $f[4], "\n"; print $f[5], "\n"; #print "\n"; $sth1->execute(@f) or die $DBI::errstr; } sub customer {my ($twig,$customer) = @_; my @f=(); $f[0] = $player->field('first_name'); $f[1] = $player->field('last_name'); $f[2] = $player->field('dob'); $f[3] = $player->field('email'); print $f[0], "\n"; print $f[1], "\n"; print $f[2], "\n"; print $f[3], "\n"; #print "\n"; $sth2->execute(@f) or die $DBI::errstr; } sub supplier {my ($twig,$supplier) = @_; my @f=(); $f[0] = $player->field('name'); $f[1] = $player->field('address'); $f[2] = $player->field('tel-no'); print $f[0], "\n"; print $f[1], "\n"; print $f[2], "\n"; #print "\n"; $sth3->execute(@f) or die $DBI::errstr; }