#!/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", -charset => "utf-8"); # change to POSTDATA my $xml = do { local $/; };#$cgi->param("POSTDATA"); my $dashes = '-' x 50 ."\n"; print qq!XML received : $dashes $xml $dashes !; my $username = "user"; my $password = "password"; my $dsn = "dbi:mysql:twig:localhost"; 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\n$dashes"; } my $twig = new XML::Twig( twig_handlers => {player => \&player, customer => \&customer, supplier => \&supplier} ); $twig->parse($xml); #$twig->print; sub player { my ($twig,$player) = @_; my @f=(); for ('name','ppg','rpg','apg','g','blk'){ push @f, $player->field($_); print "Player $_ : $f[-1]\n"; } $sth1->execute(@f) or die $DBI::errstr; print $dashes; } sub customer { my ($twig,$customer) = @_; my @f=(); for ('first_name','last_name','dob','email'){ push @f, $customer->field($_); print "Customer $_ : $f[-1]\n"; } $sth2->execute(@f) or die $DBI::errstr; print $dashes; } sub supplier { my ($twig,$supplier) = @_; my @f=(); for ('name','address','tel_no'){ push @f, $supplier->field($_); print "Supplier $_ : $f[-1]\n"; } $sth3->execute(@f) or die $DBI::errstr; print $dashes; } __DATA__ Frank Sanbeans 3/10 frank@example.com Houston,Allan6920.13.42.814 Sprewell, Latrell6919.24.54.015 Ewing, Patrick4914.610.01.068 Supplier 1
Address 1
012345