Perl DBI: Getting POST info into select query

Discussion in 'Perl' started by Mavis, Oct 3, 2004.

  1. Mavis

    Mavis Guest

    Hi,

    I'm trying to use a simple form to select by which column I will display
    output from a MySQL database.

    I'd like to display based on ORDER BY and select Written, Average, etc.

    I know the data is getting to the script from the post, I just can't get it
    to the select statement.

    Here is the code:




    #!/usr/bin/perl

    use DBI;

    $sizeOfFormInformation = $ENV{'CONTENT_LENGTH'};
    read (STDIN, $form_info, $sizeOfFormInformation);

    ($field_name, $command) = split (/=/, $form_info);

    my $dbh = DBI->connect ('DBI:mysql:eln8220s02', root) or die "Couldn't
    connect to database: " . DBI->errstr;

    my $sth = $dbh->prepare('

    select marks.Last_Name, min(marks.Score) as Minimum, max(marks.Score) as
    Maximum, avg(marks.Score) as Average, count(marks.Score) as Count from marks
    where marks.Type = "quiz" and marks.Score > 0 group by Last_Name order by
    [THIS IS WHERE I WANT TO INSERT THE POST DATA] desc

    ') or die "Couldn't prepare statement: " . $dbh->errstr;

    $sth->execute;






    Any help would be appreciated.

    Thanks.
     
    Mavis, Oct 3, 2004
    #1
    1. Advertising

  2. Why don't you use the CGI module to read the POST request?
    Let's say you have a field in your POST request called 'order_by', you
    would do:

    use CGI;
    my $query = CGI->new;
    my $order_by = $query->param('order_by');

    Check: http://www.perldoc.com/perl5.6/lib/CGI.html

    Regards,

    -Andrés

    Mavis wrote:

    > Hi,
    >
    > I'm trying to use a simple form to select by which column I will display
    > output from a MySQL database.
    >
    > I'd like to display based on ORDER BY and select Written, Average, etc.
    >
    > I know the data is getting to the script from the post, I just can't get it
    > to the select statement.
    >
    > Here is the code:
    >
    >
    >
    >
    > #!/usr/bin/perl
    >
    > use DBI;
    >
    > $sizeOfFormInformation = $ENV{'CONTENT_LENGTH'};
    > read (STDIN, $form_info, $sizeOfFormInformation);
    >
    > ($field_name, $command) = split (/=/, $form_info);
    >
    > my $dbh = DBI->connect ('DBI:mysql:eln8220s02', root) or die "Couldn't
    > connect to database: " . DBI->errstr;
    >
    > my $sth = $dbh->prepare('
    >
    > select marks.Last_Name, min(marks.Score) as Minimum, max(marks.Score) as
    > Maximum, avg(marks.Score) as Average, count(marks.Score) as Count from marks
    > where marks.Type = "quiz" and marks.Score > 0 group by Last_Name order by
    > [THIS IS WHERE I WANT TO INSERT THE POST DATA] desc
    >
    > ') or die "Couldn't prepare statement: " . $dbh->errstr;
    >
    > $sth->execute;
    >
    >
    >
    >
    >
    >
    > Any help would be appreciated.
    >
    > Thanks.
    >
    >
     
    Andres Monroy-Hernandez, Oct 3, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ulloa
    Replies:
    1
    Views:
    544
    Juha Laiho
    Jul 22, 2004
  2. Jerome Hauss
    Replies:
    0
    Views:
    184
    Jerome Hauss
    Oct 13, 2004
  3. raj9999
    Replies:
    4
    Views:
    765
    robic0
    Mar 15, 2006
  4. Replies:
    68
    Views:
    802
    Waylen Gumbal
    Jun 20, 2008
  5. ccc31807

    DBI select 'like' query

    ccc31807, Mar 10, 2009, in forum: Perl Misc
    Replies:
    3
    Views:
    165
Loading...

Share This Page