
Ryudo
New User
Dec 4, 2009, 9:08 AM
Post #1 of 1
(5257 views)
|
Data isn't inserting on simple form
|
Can't Post
|
|
#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use DBI; #print HTTP header print "Content-type:text/html\n\n"; #Variable Declarations %ERRORS; #holds errors for required field check %FORM; #will hold all for data #%PATTERNS = ( # "login" => '[A-Z][a-z]{2,25}', # "pass" => ' sub parseform { #read and decode form data my $input = <>; my @pairs = split(/&/, $input); my ($name, $value); foreach (@pairs) { ($name, $value) = split(/=/i, $_); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } } #else process form and insert data if ($ENV{REQUEST_METHOD} eq "GET") { &showRegistration; } else { &parseform; #if (&validate) #{ &insertData; #} } sub insertData { $db="users"; $user="int420_093b30"; $passwd="67009025"; $host="db-mysql.zenit"; $connectionInfo="dbi:mysql:$db;$host"; #if ( #form SQL insert $insert = qq~insert project (login, pass, name, address, telephone, email) values ('$FORM{login}','$FORM{password}','$FORM{name}','$FORM{address}','$FORM{telephone}','$FORM{email}')~; #connect to mysql $dbh=DBI->connect($connectionInfo,$user,$passwd); #prepare statement and create statement handler $sth=$dbh->prepare($insert); #execute satement handler and show successful insert if ($sth->execute()) { print"SUCCESS"; } else { print"There was an error"; } $dbh->disconnect(); } #sub check_fields #{ # my $success = 1; # foreach (keys (%FIELDS)) # { # if ($FORM{$_} !~ $PATTERNS{$_}) # { # $ERRORS{$_} = "Error: $FIELDS{$_} is missing or incorrect format\n"; # $success = 0; # } # } #return $success; #} sub showRegistration { print"<html>"; print"<head>"; print"<head>"; print"<title>"; print"Stage 1 - INT420 Project"; print"</title>"; print"<title>"; print"Stage 1 - INT420 Project"; print"</title>"; print"<form action=\"stage1.cgi\" method=\"POST\">"; print"Login - <input type=\"text\" name=\"login\"><br />"; print"Password - <input type=\"text\" name=\"password\"><br />"; print"Name - <input type=\"text\" name=\"name\"><br />"; print"Address - <input type=\"text\" name=\"address\"><br />"; print"Telephone - <input type=\"text\" name=\"telephone\"><br />"; print"Email - <input type=\"text\" name=\"email\"><br />"; print"<input type=\"submit\" />"; print"</form>"; print"</html>"; } When I hit the button to insert, I get the following error Software error: Can't call method "prepare" on an undefined value at /home/int420_093b30/httpd/cgi-bin/stage1.cgi line 72, <> line 1. can anyone help!
(This post was edited by Ryudo on Dec 4, 2009, 9:21 AM)
|