Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Calling a PERL script from an html form

by steampunk333 (Initiate)
on Jun 01, 2014 at 02:34 UTC ( [id://1088156]=perlquestion: print w/replies, xml ) Need Help??

steampunk333 has asked for the wisdom of the Perl Monks concerning the following question:

For my html script, I'm trying to call a perl script using a form action, as seen below at line 157. However, I keep getting the error message "(process:2754): GLib-CRITICAL **: g_slice_set_config: assertion 'sys_page_size == 0' failed" whenever I try to use the form, which is supposed to return the results from the perl script(shown below the html). Can anyone explain to me what I'm doing wrong, and how to fix this?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ut +f-8" /> <title>Project Page</title> </head> <body> <h1>Main Project Page</h1> <p> Relational databases are used in many large websites t +o quickly sort through information. In this case, the amount of information is more easily man +agable;<br /> when dealing with larger amounts, however, relational databases can be quite useful for organization. +<br /><br /> Below are the tables for which you can search for information from further down:</p> <table border = "2" cellpadding = "3" cellspacing = "4" wi +dth = "95%" > <caption>employees</caption> <tr> <th width = "10%">id</th> <th width = "20%">firstname</th> <th width = "20%">lastname</th> <th width = "15%">annual_salary</th> <th width = "30%">date_first_employed</th> </tr> <tr> <td width = "10%">1</td> <td width = "20%">Alan</td> <td width = "20%">White</td> <td width = "15%">100000</td> <td width = "30%">2004-09-03</td> </tr> <td width = "10%">2</td> <td width = "20%">Jake</td> <td width = "20%">Michaels</td> <td width = "15%">80000</td> <td width = "30%">2007-11-18</td> <tr> <td width = "10%">3</td> <td width = "20%">Jessica</td> <td width = "20%">Stirling</td> <td width = "15%">120000</td> <td width = "30%">1999-03-28</td> </tr> <tr> <td width = "10%">4</td> <td width = "20%">Jim</td> <td width = "20%">Schmim</td> <td width = "15%">60000</td> <td width = "30%">2012-05-17</td> </tr> </table> <br /> <br /> <table border = "2" cellpadding = "3" cellspacing = "4 +" width = "95%" > <caption>employee_contacts</caption> <tr> <th width = "20%">contact_id</th> <th width = "20%">email_address</th> <th width = "25%">home_phone_number</th> <th width = "30%">emergency_phone_number</th> </tr> <tr> <td width = "20%">Alan W.</td> <td width = "20%">Alan@mail.com</td> <td width = "25%">650-939-8504</td> <td width = "30%">650-249-7128</td> </tr> <tr> <td width = "20%">Jake M.</td> <td width = "20%">Jake@mail.com</td> <td width = "25%">650-421-7730</td> <td width = "30%">650-855-2217</td> </tr> <tr> <td width = "20%">Jessica S.</td> <td width = "20%">Jessica@mail.com</td> <td width = "25%">650-419-2350</td> <td width = "30%">650-273-8530</td> </tr> <tr> <td width = "20%">Jim S.</td> <td width = "20%">Jim@mail.com</td> <td width = "25%">650-769-8230</td> <td width = "30%">650-289-6530</td> </tr> </table> <br /> <br /> <p> The tables above are linked by this followi +ng table:</p> <br /> <br /> <table border = "2" cellpadding = "3" cellspacing += "4" width = "20%"> <caption>table_link</caption> <th width = "5%">id</th> <th width = "15%">contact_id</th> <tr> <td width = "5%">1</td> <td width = "15%">Alan W.</td> </tr> <tr> <td width = "5%">2</td> <td width = "15%">Jake M.</td> </tr> <tr> <td width = "5%">3</td> <td width = "15%">Jessica S.</td> </tr> <tr> <td width = "5%">4</td> <td width = "15%">Jim S.</td> </tr> </table> <p>The following fields can be used to search +for information <br /> found in the tables above: </p> <br /> <form action = "localhost/home/phillip/Documen +ts/Aptana Studio 3 Workspace/project/scriptdone5.pl" METHOD = "post" +target = "_blank"> Employee name:<input type = "text" size = +"25" maxlength = "25" name = "name"><br /><br /> </form> <br /><br /> <form action = "" METHOD = "post"> Employee id:<input type = "text" size = "2 +5" maxlength = "25" name = "id"><br /><br /> </form> <br /><br /> <form action = "" METHOD = "post"> Employee email address:<input type = "text +" size = "25" maxlength = "25" name = "email_address"><br /><br /> </form> <br /><br /> <form action = "" METHOD = "post"> Date first employed:<input type = "text" s +ize = "25" maxlength = "25" name = "date_first_employed"><br /><br /> </form> </body> </html> #!/usr/bin/perl use DBI; use warnings; my $firstname = $in{name}; my $db = "project_database"; my $host = "localhost"; my $user = "root"; my $pw = "steampunk333"; my $source = "DBI:mysql: database = project_database; host = localhost +;"; my $contactid; my $dbh; print "Content-type:text/html\n\n"; $dbh = DBI->connect($source,$user,$pw) or die "Can't connect to database: $DBI::errstr\n"; my $qfirstname = dbh->quote($firstname); if ($firstname eq "Alan") {$contactid = "Alan W.";} elsif ($firstname eq "Jake") {$contactid = "Jake M.";} elsif ($firstname eq "Jessica") {$contactid = "Jessica S.";} elsif ($firstname eq "Jim") {$contactid = "Jim S.";} else {$contactid = "0"; print "<p><br />Error: No Matches were found for your search. Did you +include capital letters?</p><br /><br />";} if($contactid ne "0") {print "<br /><br /><p>This is the information returned from your quer +y:</p><br /><br />";} my $qcontactid = dbh->quote($contactid); my $sth = $dbh->prepare("SELECT lastname, date_first_employed, annual_ +salary FROM employees WHERE firstname = $qfirstname, SELECT * FROM employee_contacts WHERE contact_id = $qcontactid"); $sth->execute( ); while (($lastname, $date_first_employed, $annual_salary, $date_first_e +mployed, $email_address, $home_phone_number, $emergency_phone_number) = $sth->f +ethrow_array()) { print "<p>Employee ID: $id<br /><br />Last Name: $lastname<br /><b +r /> Date First Employed: $date_first_employed<br /><br /> Annual Salary: $annual_salary<br /><br />Contact ID: $contactid<br + /><br /> Email Address: $email_address<br /><br />Home Phone Number: $home_ +phone_number<br /><br /> Emergency Phone Number: $emergency_phone_number</p><br /><br />"; } $sth->finish( ); $dbh->disconnect( );

Replies are listed 'Best First'.
Re: Calling a PERL script from an html form
by Old_Gray_Bear (Bishop) on Jun 01, 2014 at 04:31 UTC
    You said:
    Can anyone explain to me what I'm doing wrong, and how to fix this?
    Well, there are some questions to be exammined here:
    • What message do you get when you run the perl code outside of your CGI environment?
    • What happens when you run your CGI code with the Perl code stubbed out? (Mock up a set of return values and return them with out benefit of any DB calls.)
    • What is 'process 2754'? And which call in glibc is going walk-about? Glibc is the Gnu implementation of the C Standard Interfaces library and it sounds like there is a low-level system call with somthing funny in the parameter list.
    You have a three part contraption here involving CGI, a Database engine, and some Perl gluing the various part together. You need to isolate which leg of the tripod is broken. If I was to offer a wild guess, I bet that something being passed to the DBI driver is wonky. But I wouldn't bet my mornng cup of coffee on it.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Calling a PERL script from an html form
by poj (Abbot) on Jun 01, 2014 at 18:16 UTC

    Apart from these small mistakes

    dbh->quote # missing $ on $dbh $sth->fethrow_array() # missing c in fetch
    the major problem is the SQL. For it to work you have to JOIN the tables and the type of JOIN, LEFT, RIGHT, INNER, OUTER etc depends on the design of your tables, essentially what the primary keys are. I've created a simplified working example of your code so you can experiment with different SQL statements ;

    #!/usr/bin/perl use strict; use warnings; use DBI; use CGI ':standard'; use CGI::Carp 'fatalsToBrowser'; my $firstname = param('name') || 'Alan';# remove || Alan when working my $user = "user"; my $pw = "password"; my $source = "DBI:mysql:test;localhost;"; my $dbh = DBI->connect($source, $user, $pw, {RaiseError => 1, PrintError => 1}) or die ("Error connecting $DBI::errstr"); print "Content-type:text/html\n\n"; print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Search Results</title> </head> <body>'; # SQL my $sth = $dbh->prepare(" SELECT e.id, firstname, lastname, date_first_employed, annual_salary, c.contact_id, email_address, home_phone_number, emergency_phone_number FROM employees as e LEFT JOIN contact_id as i ON i.id = e.id INNER JOIN employee_contacts as c ON c.contact_id = i.contact_id WHERE firstname = ? "); $sth->execute( $firstname ); # Results my $records = 0; while ( my ($id, $firstname, $lastname, $date_first_employed,$annual_salary, $contact_id,$email_address, $home_phone_number, $emergency_phone_number) = $sth->fetchrow_array()) { print "<p>This is the information returned from your query: </p> <pre>Firstname = $firstname</pre>"; ++$records; print "<pre>Employee ID: $id First Name: $firstname Last Name: $lastname Date First Employed: $date_first_employed Annual Salary: $annual_salary Contact ID: $contact_id Email Address: $email_address Home Phone Number: $home_phone_number Emergency Phone Number: $emergency_phone_number</pre><hr />"; } $sth->finish( ); $dbh->disconnect( ); if ($records == 0) { print "<p>Error: No Matches were found for your search for Firstname = $firstname. Did you include capital letters?</p>"; } # Another Search print '<b>Search for Name [ Alan Jake Jessica Jim ] </b> <form action="" method="post"> <input type="text" name="name"/> <input type="submit" value="Search"/> </form> </body></html>';
    poj
Re: Calling a PERL script from an html form
by InfiniteSilence (Curate) on Jun 01, 2014 at 14:49 UTC

    You didn't provide enough of your Perl source code to solve your problem. When I copied your code to my machine I instantly got errors regarding the %in hash so I replaced that with the ever-popular CGI module:

    use CGI qw|:standard|; #my $firstname = $in{name}; + my $firstname = CGI::param('name');

    Now to get it to compile cleanly you have other problems:

    use strict; #<- get into the habit of using this #... my ($lastname, $date_first_employed, $annual_salary, $id , $email_address, $home_phone_number, $emergency_phone_number); while (($lastname, $date_first_employed, $annual_salary, $id, $email_address, $home_phone_number, $emergency_phone_number) = $sth->f +ethrow_array()) #...

    You are using $date_first_employed multiple times in while statement (I changed it) and although you were using $id in your print statement on line 61 you had not defined or set it to anything. Strict would have complained about these things.

    Wow, my SQL skills must be getting rusty when I see your query -- from where I'm sitting that isn't ever going to work:

    "SELECT lastname, date_first_employed, annual_salary FROM employees WH +ERE firstname = $qfirstname, SELECT * FROM employee_contacts WHERE contact_id = $qcontactid")

    These are two totally separate SQL statements comma separated. Is there database engine that allows that? If so please let me know.

    Celebrate Intellectual Diversity

Re: Calling a PERL script from an html form
by fishmonger (Chaplain) on Jun 01, 2014 at 15:05 UTC

    Why are you defining 4 separate form blocks each with only a single and related input field but none of them have a submit field?

    I see a possible problem with the value of the action attribute on the first form which is the line 157 you referred to.

    <form action = "localhost/home/phillip/Documents/Aptana Studio 3 Workspace/project/scriptdone5.pl" METHOD = "post" target = "_blank">

    Somehow I doubt that the path you specified is correct i.e., it's not based/rooted off of your DocumentRoot.

Re: Calling a PERL script from an html form
by Anonymous Monk on Jun 01, 2014 at 14:15 UTC
    My initial guess would be that there is something fundamentally-wrong in the setup of your environment – not with this Perl code. If an error message involving "glibc" is being thrown, the web server is probably not getting that far.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1088156]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found