homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Webform Perl Script
dmcintosh




msg:3993856
 12:57 am on Sep 23, 2009 (gmt 0)

I've been working on this bloody script for 3 days and can't work it out, the basis of it, uses a HTML webform to post data to a MYSQL Database. I've tried the Post method and the Get method.

I'm able to write to the DB when i use a static value, when i use the $_GET method it enumerates the webform values to the URL, but then not to the DB. i have tried to debug using values like print $_GET; but nothing is displayed, i can't work out why it will pick up the values, but won't parse them to the DB. $_POST is the same, when you use print $_POST['value'] is prints ''

PLEASE HELP- thanks in advance

CODE:

#!/usr/bin/perl --

# Create the necessary headers
print "Content-type: text/html\n\n";

# Use the DBI module
use DBI;
use CGI::Carp qw/fatalsToBrowser/;

# Declare local variables

my ($databaseName, $databaseUser, $databasePw, $dbh);
my ($stmt, $sth, @newRow);

$Phone = $_REQUEST["Phone"];
$Email = $_REQUEST["Email"];
$Enquiry = $_REQUEST["Enquiry"];
$FistName = $_REQUEST["FirstName"];
$Address1 = $_REQUEST["Address1"];
$Address2 = $_REQUEST["Address2"];
$Suburb = $_REQUEST["Suburb"];
$State = $_REQUEST["State"];
$Postcode = $_REQUEST["Postcode"];
$Authorised = $_REQUEST["Authorised"];

# Connect to the database
# Note this connection can be used to
# execute more than one statement
# on any number of tables in the database

$dbh = DBI->connect("DBI:mysql:database=complete_Enquiry;host=localhost", "complete_DB", 'passwordremoved')
die "Connect failed: $DBI::errstr\n";

print $Phone

# Create the statement.
#$stmt = "INSERT INTO Client #(Phone,Email,Enquiry,FirstName,Surname,Address1,Address2,Suburb,State,Postcode,Authorised)
#VALUES('$_GET[Phone]','$_GET[Email]','$_GET[Enquiry]','$_GET[FirstName]','$_GET[Surname]',
#'$GET_[Address1]','$_GET[Address2]','$_GET[Suburb]','$_GET[State]','$_GET[Postcode]','$_GET[Authorised]')";

#) VALUES ('"$_POST['FirstName']"', '". $_POST['Surname']."', '".$_POST['Phone']."','". $_POST['email'] #."','".$_POST['Address1']."','".$_POST['Address2']."','".$_POST['Suburb']."','".$_POST['State']."','".$_POST['Postcode']."',
#'".$_POST['Enquiry']."','".$_POST['Authorised']."');

#$_POST['FirstName'],$_POST['Surname'],$_POST['Phone'],$_POST['Email'],
#$_POST['Address1'],$_POST['Address2'],$_POST['Suburb'],$_POST['State'],$_POST['Postcode'],
#$_POST['Enquiry'],$_POST['Authorised'])";

# Prepare and execute the SQL query
#$sth = $dbh->prepare($stmt) die "prepare: $stmt: $DBI::errstr";
#$sth->execute die "execute: $stmt: $DBI::errstr";

#print "complete";

# Clean up the record set and the database connection
#$sth->finish();
#$dbh->disconnect();

[edited by: phranque at 6:36 am (utc) on Sep. 23, 2009]
[edit reason] fixed sidescroll [/edit]

 

phranque




msg:3993954
 6:34 am on Sep 23, 2009 (gmt 0)

welcome to WebmasterWorld [webmasterworld.com], dmcintosh!

from the perlop man page [perldoc.perl.org]:
Single quotes indicate the text is to be treated literally with no interpolation of its content.

hard to tell which commented code was causing which problem but this may be part of it.

the major issue you are having is that you are trying to use php methods in perl.
unless you have added something you haven't shown, the _POST, _GET & _REQUEST arrays are not native perl nor supported by the CGI module as far as i know.

try this:
FETCHING THE VALUE OR VALUES OF A SINGLE NAMED PARAMETER [perldoc.perl.org]

dmcintosh




msg:3994003
 9:46 am on Sep 23, 2009 (gmt 0)

Thats what i originally thought, and i only went to post get and request as $query->param gives me a 500 error.

the $query->param version of my script is below, i'm only using the print command to enumerate the variables, before i put the DB push module in.

#!/usr/bin/perl --

# Create the necessary headers
print "Content-type: text/html\n\n";

use CGI;
$query=new CGI;

select(STDOUT); $¦=1; # make unbuffered
require 5;

# Declare local variables

#variables for database parse DO NOT DELETE
#my ($databaseName, $databaseUser, $databasePw, $dbh);
#my ($stmt, $sth, @newRow);

$Phone = $query->param("Phone");
$Email= $query->param("Email")
$Enquiry= $query->param("Enquiry")
$FirstName = $query->param("CompanyName");
$Surname = $query->param("Address1");
$address1 = $query->param("Address2");
$address2 = $query->param("Address3");
$Suburb = $query->param("Suburb");
$State = $query->param("State");
$Postcode= $query->param("Postcode")

@names = $query->param($Phone);

print $Phone;

dmcintosh




msg:3994014
 10:08 am on Sep 23, 2009 (gmt 0)

And i did actually fix up the string terminators before running the script so thats not the issue :)

dmcintosh




msg:3994024
 10:20 am on Sep 23, 2009 (gmt 0)

made the following changes:

use CGI::Carp qw/fatalsToBrowser/;
AND
my @parameters = $cgi->param; #gets the names of all form fields
my %params = $cgi->Vars

now rcving: Can't locate object method "new" via package "CGI" at webform.cgi line 6

phranque




msg:3994311
 6:26 pm on Sep 23, 2009 (gmt 0)

you probably don't have the CGI module installed properly or the module library path is not specified in the @INC global array.

dmcintosh




msg:3994350
 7:47 pm on Sep 23, 2009 (gmt 0)

i only realised after my post, use

CGI::Carp qw/fatalsToBrowser/; doesn't use the cgi module, when i put in use CGI;

i get: Can't call method "param" on an undefined value at webform.cgi line 19.

line 16 is commented out, i've put the non functioning code seperately so no one gets confused :P

use CGI;
use CGI::Carp qw/fatalsToBrowser/;
$query=new CGI('');

# Declare local variables

#variables for database parse DO NOT DELETE
#my ($databaseName, $databaseUser, $databasePw, $dbh);
#my ($stmt, $sth, @newRow);

my @parameters = $cgi->param; #gets the names of all form fields
my %params = $cgi->Vars; #stores all form fields as name/value pairs in a hash

$Phone = $query->param("Phone");
$Email= $query->param("Email");
$Enquiry= $query->param("Enquiry");
$FirstName = $query->param("CompanyName");
$Surname = $query->param("Address1");
$address1 = $query->param("Address2");
$address2 = $query->param("Address3");
$Suburb = $query->param("Suburb");
$State = $query->param("State");
$Postcode= $query->param("Postcode");

Print $Phone;

I used to know this stuff, this is soooo frustrating- that'll teach my to go from coding to Infrastructure consulting... lol

phranque




msg:3994359
 8:10 pm on Sep 23, 2009 (gmt 0)

i get: Can't call method "param" on an undefined value at webform.cgi line 19.
...
use CGI;
$query=new CGI('');
...
my @parameters = $cgi->param; #gets the names of all form fields

try this:
use CGI;
my $cgi = new CGI;
my @parameters = $cgi->param;

dmcintosh




msg:3994425
 9:10 pm on Sep 23, 2009 (gmt 0)

getting a 500 again...aaarrrrgggghhhhh!

phranque




msg:3994641
 5:25 am on Sep 24, 2009 (gmt 0)

a 500 response usually means there are clues in the server error log.

dmcintosh




msg:3994746
 7:38 am on Sep 24, 2009 (gmt 0)

unfortunately this is a hosted server, and they won't give me shell access :(

the cpanel error log only has

[Thu Sep 24 07:09:11 2009] [error] [client 124.168.158.234] Premature end of script headers: /home/complete/public_html/cgi-bin/webform.cgi
[Thu Sep 24 07:08:17 2009] [error] [client 124.168.158.234] Premature end of script headers: /home/complete/public_html/cgi-bin/webform.cgi

phranque




msg:3994751
 7:47 am on Sep 24, 2009 (gmt 0)

that message means either something was written to STDOUT before the headers were written or that nothing at all was written to STDOUT.
make sure the header is output before the first executable statement in your script:
print "Content-type: text/html\n\n";

dmcintosh




msg:3994908
 2:10 pm on Sep 24, 2009 (gmt 0)

already defined, after #!/usr/bin/perl --

rocknbil




msg:3995162
 7:23 pm on Sep 24, 2009 (gmt 0)

Another cause of a premature end of script error without a specific error can be that the file was edited in a non-Unix program, such as Notepad (or . . .SimpleText, by your nickname?), and the server doesn't recognize the line return characters.

I **have heard** that the FTP clients should manage this on upload but have never seen it work. I use HomeSite in which I can set up to save files as Unix files.

If I save a file in PC format to send to someone I know will open in Windows Notepad, then forget to change it back and edit a perl script, it will 500.every.single.time.

There should be/are some freebie text editors out there that can save in Unix format, one less thing to worry about.

Testing and debugging offline is critical. Download and install Perl on your local system, you can then open a command prompt, navigate to the directory, and type perl yourscript.cgi. This will eliminate direct errors in the script.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved