Welcome to WebmasterWorld Guest from 54.167.86.211

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Webform Perl Script

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

New User

5+ Year Member

joined:Sept 23, 2009
posts: 8
votes: 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]

6:34 am on Sept 23, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10544
votes: 8


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]

9:46 am on Sept 23, 2009 (gmt 0)

New User

5+ Year Member

joined:Sept 23, 2009
posts: 8
votes: 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;

10:08 am on Sept 23, 2009 (gmt 0)

New User

5+ Year Member

joined:Sept 23, 2009
posts:8
votes: 0


And i did actually fix up the string terminators before running the script so thats not the issue :)
10:20 am on Sept 23, 2009 (gmt 0)

New User

5+ Year Member

joined:Sept 23, 2009
posts: 8
votes: 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

6:26 pm on Sept 23, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10544
votes: 8


you probably don't have the CGI module installed properly or the module library path is not specified in the @INC global array.
7:47 pm on Sept 23, 2009 (gmt 0)

New User

5+ Year Member

joined:Sept 23, 2009
posts: 8
votes: 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

8:10 pm on Sept 23, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10544
votes: 8


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;
9:10 pm on Sept 23, 2009 (gmt 0)

New User

5+ Year Member

joined:Sept 23, 2009
posts: 8
votes: 0


getting a 500 again...aaarrrrgggghhhhh!
5:25 am on Sept 24, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10544
votes: 8


a 500 response usually means there are clues in the server error log.
7:38 am on Sept 24, 2009 (gmt 0)

New User

5+ Year Member

joined:Sept 23, 2009
posts:8
votes: 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

7:47 am on Sept 24, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10544
votes: 8


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";
2:10 pm on Sept 24, 2009 (gmt 0)

New User

5+ Year Member

joined:Sept 23, 2009
posts:8
votes: 0


already defined, after #!/usr/bin/perl --
7:23 pm on Sept 24, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 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.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members