Forum Moderators: coopster & phranque

Message Too Old, No Replies

CGI and DBI

         

MartinTygsen

1:21 pm on Aug 14, 2005 (gmt 0)

10+ Year Member



Hello WW,

I'm a newbie to both CGI and Perl, but I have tried to make a little script with Perl that connects to a mysql database.

It works fine from a konsol, but when I try on the net with som CGI I get a 500 error, I probaly made som big mistakes but Im stuck here.

My script looks like this

#!c:/perl/bin/perl.exe
use strict;
use CGI ':standard';
use DBI;

my $DataBaseName = "dbname";
my $DataBaseHost = "host";
my $DataBaseUser = "user";
my $DataBasePass = "pass";

my $DataHandle = DBI->connect("DBI:mysql:database=$DataBaseName;host=$DataBaseHost",
"$DataBaseUser",
"$DataBasePass",
{ RaiseError => 1,
AutoCommit => 0 }) ¦¦ die "Unable to connect to $DataBaseHost because $DBI::errstr";

$DataHandle->do("CREATE TABLE bookmark (id INTEGER(5) NOT NULL auto_increment,
title VARCHAR(40) default NULL,
link VARCHAR(40) default NULL,
decrip VARCHAR(40) default NULL,
KEY id (id)
)");

my $title = param('title');
my $link = param('link');
my $descrip = param('descrip');

$DataHandle->do("INSERT INTO bookmark values(id,'$title','$link','$descrip')");

$DataHandle->disconnect();

-------------------------------------------------
and the HTML

<form name="bookmark" method="post" action="../cgi-bin/index.pl">
<p>
Titel :
<input type="text" name="title">
</p>
<p>
Link :
<input type="text" name="link">
</p>
<p>Beskrivelse:<br>
<textarea name="descrip"></textarea>
</p>
<p>
<input type="reset" name="Reset" value="Reset">
<input type="submit" name="Submit" value="tilf&oslash;j">
</p>
</form>

I hope someone could point me in the right direction again, thanks.

[edited by: jatar_k at 4:00 pm (utc) on Aug. 14, 2005]
[edit reason] generalized connection info [/edit]

WWMike

1:31 pm on Aug 14, 2005 (gmt 0)

10+ Year Member



This line probably needs to be changed to run it on the web:

#!c:/perl/bin/perl.exe

Try this:

#!/usr/bin/perl

If that doesn't solve it you might also want to include the following line to generate a required header:

print "Content-type: text/html\n\n";

MartinTygsen

1:37 pm on Aug 14, 2005 (gmt 0)

10+ Year Member



The first line it because its a Windows server with apache I run, not a unix server.

Other scripts work fine with #!c:/perl/bin/perl.exe

Jeg tried the content-Type but i doesn't help.

Have you other suggentions?

WWMike

1:55 pm on Aug 14, 2005 (gmt 0)

10+ Year Member



Well, if other scripts run then comment out every line in your script and including them one at a time from the top down to see where the problem is. It might be the DBI module trying you're trying to use that is not installed.

MartinTygsen

2:04 pm on Aug 14, 2005 (gmt 0)

10+ Year Member



NO the DBI modul is installed.

If I replace the param() function and just replace it with normal words, and then run my script in a console, it works fine, it creates the tabel and put the values in the table.

But I want it to work with a simple html form so when i add something it the forms it will add it to the database.

I hope you understand.

WWMike

2:20 pm on Aug 14, 2005 (gmt 0)

10+ Year Member



I'm not familiar with module syntax but don't you need to use something like:

$query = new CGI;
$value = $query->param('foo');

Look here:

[search.cpan.org...]

MartinTygsen

2:31 pm on Aug 14, 2005 (gmt 0)

10+ Year Member



thanks, I will look at your link

KevinADC

5:41 pm on Aug 14, 2005 (gmt 0)

10+ Year Member




I'm not familiar with module syntax but don't you need to use something like:

$query = new CGI;
$value = $query->param('foo');

that's only if you are using the OO method. If you are using the "standard" method:

use CGI qw':standard';

then you don't have to create a new object and you can just use the function style syntax:

use CGI qw/:standard/;
print header(),start_html();
my $string = param('foo');
print $string;
print end_html();

it's all in the CGI documentation.