All pastes #901783 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-02-12 22:39 UTC
rendered paste body
#!/usr/bin/perl
use CGI;
use DBI;
use strict;

my $req = new CGI;
my $BDDriver = "DBI:Oracle:host=sunensc.uqac.ca;sid=dimbd;port=1521";
my $BD_Username ="NULL";
my $BD_Password ="NULL";

print $req->header();
print $req->start_html("Resultat de la request");

if ($req->request_method() eq "POST") {
    my @contactInfo = ($req->param("nom"),$req->param("telephone"),$req->param("courriel"));
    if ( saveContact(@contactInfo) eq 0 )
    {
        print "Contact ".$req->param("nom")." saved successfully:";
    }
    showContacts();
}
else {
    print $req->h1("Error: unknown method used");
}

print $req->end_html;

sub saveContact
{
    my @contactInfo = @_;
    if (@contactInfo[0] eq "" || @contactInfo[1] eq "" || @contactInfo[2] eq "" ) {
        print $req->h1("Error: contact information are missing");
    }
    else
    {        
        # On enregistre dans la BD
        my $dbh = DBI->connect($BD_Driver, $BD_Username, $BD_Password) or die "Database connection could not be made: $DBI::errstr\n";
        my $sth= $dbh->prepare("INSERT INTO contacts (nom,telephone,courriel) VALUES ('".@contactInfo[0]."','".@contactInfo[1]."','".@contactInfo[2]."')") 
            or die "Could not save contact: $DBI::errstr\n";
        $sth->execute();
        $sth->finish;
        $dbh->disconnect;
        return 0;
    }
}

sub showContacts
{
    my $dbh = DBI->connect($BD_Driver, $BD_Username, $BD_Password) or die "Database connection could not be made: $DBI::errstr\n";
    my $sth= $dbh->prepare("SELECT * FROM contacts")
        or die "Could not save contact: $DBI::errstr\n";
    $sth->execute();
    print "<br/>";
    while (my @ary = $sth->fetchrow_array) {
        foreach (@ary) {
            print "$_ ";
        }
        print "<br/>";
    }
    $sth->finish;
    $dbh->disconnect;
    return 0;
    
}