Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2004
    Location
    Melbourne
    Posts
    14

    Unanswered: explain what the following code does ?

    #!/usr/local/bin/perl
    use DBI;
    $db = "names";
    $host = "localhost";
    $db_user = "user";
    $db_pass = "password";

    $dbh = DBI->connect ( "DBI:mysql:database=$db;host=$host",$db_user,$db_p ass) ||
    die("Cannot connect to database");

    @input = split (/&/,<STDIN>);
    foreach $key_value(@input){
    ($key,$value) = split (/=/,$key_value);
    $value =~ tr/+/ /;
    $value =~ s/%(..)/pack("c",hex($1))/ge;
    $FORM{$key} = $value

    }

    $id = $FORM{ 'id' };
    if($id eq ""){
    &fail();
    exit(0);
    }
    $sth =$dbh ->prepare("select surname, givenname, email from staff where id='$id' ");
    $sth->execute || die("Cannot execute query: $dbh->errstr");

    if($sth->rows <1){
    &fail();
    exit(0);
    $sth->finish();
    }

    for ($sth->rows -1){
    @rows = $sth->fetchrow_array;
    }

    &header();
    print "The following details were found for ID <b>$id</b>:<p>\n";
    print "Name: $row[0], $row[1]<br>\n";
    print "Email: $row[2]<br>\n";
    &footer();

    $sth->finish();
    $dbh->disconnect();

    sub fail{
    &header();
    print "a problem occurred with the input \n";
    &footer();
    }

    sub header{
    print "Content-type:text/html\n\n";
    print "<html>\n";
    print "<head>\n";
    print "<title>Search Results</title>\n";
    print "</head>\n";
    print "<body bgcolor=\"#d0d0d0">\n";
    }

    sub footer{
    print "</body>\n";
    print "</html>\n";
    }

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    #!/usr/local/bin/perl # this is the shebang. It's the path to the Perl-interpreter
    use DBI; # DBI is a Perl-Modul for using databases
    $db = "names"; # name of database
    $host = "localhost"; # host, where the database is
    $db_user = "user";
    $db_pass = "password";

    $dbh = DBI->connect ( "DBI:mysql:database=$db;host=$host",$db_user,$db_p ass) ||
    die("Cannot connect to database"); # establish a connection to the database

    #-------------------------------------------#
    # the following code parses the #
    # parameters sent by a HTML-form. #
    # It creates a hash with the names of #
    # the form elements as the key and #
    # the user-input as value #
    #-------------------------------------------#
    @input = split (/&/,<STDIN> );
    foreach $key_value(@input){
    ($key,$value) = split (/=/,$key_value);
    $value =~ tr/+/ /;
    $value =~ s/%(..)/pack("c",hex($1))/ge;
    $FORM{$key} = $value

    }

    # if no id is given print an errormessage
    $id = $FORM{ 'id' };
    if($id eq ""){
    &fail();
    exit(0);
    }

    $sth =$dbh ->prepare("select surname, givenname, email from staff where id='$id' "); # prepare a query on the database
    $sth->execute || die("Cannot execute query: $dbh->errstr"); # do the query

    # if no entry was found print errormessage
    if($sth->rows <1){
    &fail();
    exit(0);
    $sth->finish();
    }

    for ($sth->rows -1){
    @rows = $sth->fetchrow_array;
    }

    &header();

    # print the entries
    print "The following details were found for ID <b>$id</b>:<p>\n";
    print "Name: $row[0], $row[1]<br>\n";
    print "Email: $row[2]<br>\n";
    &footer();

    $sth->finish();
    $dbh->disconnect(); # close connection

    # this subroutine prints an errormessage
    sub fail{
    &header();
    print "a problem occurred with the input \n";
    &footer();
    }


    # it print a header, it is required
    sub header{
    print "Content-type:text/html\n\n";
    print "<html>\n";
    print "<head>\n";
    print "<title>Search Results</title>\n";
    print "</head>\n";
    print "<body bgcolor=\"#d0d0d0">\n";
    }

    # the footer closes the html-tags...
    sub footer{
    print "</body>\n";
    print "</html>\n";
    }


    It is not the best code. I would recommend to use the CGI-module to ensure the best way of parsing the parameters sent by the HTML-form. I would also recomment to use 'use strict;' to avoid misspellings...
    So my code would something like this:
    Code:
    #! /usr/bin/perl
    
    use strict; # now you have to declare all variables with 'my'
    use warnings; # to get additional warnings
    use diagnostics;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser); # to see the errormessages on the brwoser
    use DBI;
    
    my $cgi = CGI->new(); # create a new instance of CGI
    my %FORM = $cgi->Vars(); # parses the parameters
    print $cgi->header(-type => 'text/html'); # print the header
    
    print $cgi->start_html(-title => 'Search Results'); # does the same like your sub header()
    
    my $user = 'user';
    my $pwd  = 'password';
    my $db    = 'database';
    my $host = 'localhost';
    
    my $statment = 'SELECT surname, givenname, email FROM staff where id= ?';
    # the questionmark is replaced by a value in $sth->execute
    
    unless($FORM{id}){
      print_error("No ID given!");
    }
    
    my $dbh = DBI->connect("DBI:mysql:$db:$host",$user,$pwd) or print_error($DBI::errstr);
    my $sth = $dbh->prepare($statment);
    $sth->execute($FORM{id});
    while(my ($surname,$givenname,$email) = $sth->fetchrow_array()){
      print "The following details were found for ID <b>$FORM{id}</b>:<p>\n";
      print "Name: $surname, $givenname<br>\n";
      print "Email: $email<br>\n";
    }
    $sth->finish();
    $dbh->disconnect;
    
    print $cgi->end_html;
    
    sub print_error{
      my ($message) = @_; # gets the parameters
      print $cgi->h2($message);
      exit;
    }
    board.perl-community.de - The German Perl-Community

  3. #3
    Join Date
    Jan 2004
    Location
    Melbourne
    Posts
    14

    Thank you

    hello reneeb,

    Thank you for the solution.

    bye

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •