Perl and Mysql help!!!!!!!!!!!!

Pastore

Diamond Member
Feb 9, 2000
9,728
0
76
I've got this registration script I'm trying to work on, and I just can't get the stupid code working!

html is:

<html><head><title>Register</title></head>
<body>
<font size=4>Register for the Address Book</font>
<form method="post" action="register.pl">
<p>User <input name="nameBox">

Pass <input type="password" name="passBox">
<p><input type="submit" value="Submit">
</form>
</body>
</html>

perl script is:

#!usr/bin/perl
#This version of the registration script uses the mysql backend

use DBI;
use CGI qw:)standard);

print header;
print start_html('Registration Successful');
print "You have successfully registered!";
print "<a href=http://157.62.24.146/~pastorem/dbtest/login.html>Click here</a>to login!";
print end_html;

$name = param('nameBox');
$pass = param('passBox');

$dbh = DBI->connect("DBI:mysql:pastorem","pastorem@localhost","pastorem");
$sth = $dbh->prepare("insert into users values ('$name','$pass')");

$sth->execute;



Any ideas what the problem might be? I know the wrx permissions are set correctly, and my perl syntax checker says it's clean. You can see it for yourself atlink
Thanks!
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Does it work if you run the SQL query from somewhere else? You sure you're conencting to the database properly?

Also, printing "You have successfully registered!" before you even attempt to connect to the database is a tad premature, not to mention the other things you left out :p

I always connect like this:
# Connect to the database
my $dsn1 = "DBI:mysql:database=dbname;host=localhost;";
my $database = DBI->connect($dsn1, 'username', 'password', { RaiseError => 1, AutoCommit => 1 });

Edit: Didn't actually look at the link until now.

Have you bothered to look at the server error log and see what error you're getting?

Does it run from the command line?
 

Pastore

Diamond Member
Feb 9, 2000
9,728
0
76
#!usr/bin/perl
#testing the database

use DBI;

print "Enter a UserID: ";
chomp( $name = <STDIN>);
print "Enter a Password for this UserID: ";
chomp( $pass = <STDIN>);

my $dbh = DBI->connect("DBI:mysql:pastorem","pastorem@localhost","pastorem");
my $sth = $dbh->prepare("insert into users values ('$name','$pass')");

$sth->execute;

That script works just fine from telnet. And I just put in that crap so It would output something to see if it was running it. What else did I leave off?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
the error you're getting is "premature end of script headers" it doesn't say anything about MySQL. That's what you should be troubleshooting.
 

Pastore

Diamond Member
Feb 9, 2000
9,728
0
76
[Tue Oct 28 20:42:02 2003] [error] [client 157.62.211.61] Premature end of script headers: register.pl, referer: http://157.62.24.146/~pastorem/dbtest/register.html
[Tue Oct 28 20:42:02 2003] [error] [client 157.62.211.61] failed to open log file, referer: http://157.62.24.146/~pastorem/dbtest/register.html
[Tue Oct 28 20:42:02 2003] [error] [client 157.62.211.61] fopen: Permission denied, referer: http://157.62.24.146/~pastorem/dbtest/register.html

That's all I get from the log file. I can't really decipher anything from that?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Your script is suppsoed to send a HTTP header string to the webserver. typically the CGI module does this when you use "print header". If this does NOT get done, then you get a errors relating to your headers. This is why I want to see what the output of your script is (not a different script that connects to the same database) from the command line. It should begin with:

Content-Type: text/html; charset=ISO-8859-1

Or similar.
 

Pastore

Diamond Member
Feb 9, 2000
9,728
0
76
[pastorem@localhost dbtest]$ perl register.pl
Content-Type: text/html; charset=ISO-8859-1

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head><title>Registration Successful</title>
</head><body></body></html>
[pastorem@localhost dbtest]$

That's the output I get when I run it from the command line, so it is definitely sending that header...
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
I don't suppose you want to give me access to your server to look at it? I've sorted out many of these stupid errors and they're often some odd thing that isn't considered, like having windows newlines on a unix server machine.
 

Pastore

Diamond Member
Feb 9, 2000
9,728
0
76
Well I'll be damned... I just added -w on the shebang line for the hell of it and the damn script ran! I have no idea why that made it work, but thanks so much for your help man, I appreciate it.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Beast1284
Originally posted by: notfred
Are you FTPing the file in binary mode from a windows box to a unix box?

yes.........

That's why it doesn't work. The shell that's running your script sees the shebangline as:

#!/usr/bin/perl\n\r#This version of the registration script uses the mysql backend\n\r...

etc.

It can't find the interpreter.

Upload it as text, the newlines will be converted to unix format, the shell will recognize it correctly, and it will work properly.

The reason it works with the -w is because the shell sees the rest of the line as extra switches that it's ignoring.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
No problem :)

I've also got a few more suggestions for your script:

"use strict" <-- keeps you out of trouble with variable scope and such.

You also probably want to check to see if a name already exists in the database before adding it :)

Let me know if you've got any more questions.
 

Pastore

Diamond Member
Feb 9, 2000
9,728
0
76
Originally posted by: notfred
No problem :)

I've also got a few more suggestions for your script:

"use strict" <-- keeps you out of trouble with variable scope and such.

You also probably want to check to see if a name already exists in the database before adding it :)

Let me know if you've got any more questions.

I'd use the use strict module but I like the fact I don't have to declare my variables. Having a C++ background it's nice not having to. About the names, well I am just kind of throwing myself into this so I'll worry about that when I get the web app i'm working on programmed, heh.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Well, just FYI, not using "use strict" is considered bad practice among perl programmers. You can still do "my $var = whatever" with "use strict" in place.

Of course, you're not obligated to use it.