Note A hash or an associative array is an array that stores items in a name=value
pair.
$Vendor_Name = $postInputs{'Vendor_Name'};
$Address = $postInputs{'Address'};
$Address2 = $postInputs{'Address2'};
$State = $postInputs{'State'};
$City = $postInputs{'City'};
$Zip = $postInputs{'Zip'};
$Phone = $postInputs{'Phone'};
$E_Mail = $postInputs{'E_Mail'};
$URL = $postInputs{'URL'};
$Contact = $postInputs{'Contact'};
This segment of code takes the associative array that was returned from readPostInput() and
assigns the values to variables. This is done to make the code easier to read and understand. You
could have easily skipped this step and used the array. The only problem is that your SQL statement
would be incredibly long and hard to read. This step saves you a lot of eyestrain later. Notice that the
names in the array are the names that were used on the Web page. This is important to remember
because Perl is case sensitive. A misspelling here could mean hours of debugging later. So take your
time and be cautious.
$dbh->do("INSERT INTO Vendors VALUES(NULL,'$Vendor_Name', '$Address', '$Address2', '$City',
'$State', '$Zip', '$Phone', '$E_Mail', '$URL', '$Contact')");
This code segment creates the SQL statement and executes it on the database. The values that were in
the text boxes on your form are now being added to the database. Seems a little too easy, doesn't it?
$dbh->disconnect;
Because you are all done with the database handle, you need to clean up. Disconnect from the
database to free up system resources and the connection to the database.
sub readPostInput{
my (%searchField, $buf, $pair, @pairs);
if ($ENV{'REQUEST_METHOD'} eq 'POST'){
read(STDIN, $buf, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buf);
foreach $pair(@pairs){
($name, $val) = split(/=/, $pair);
$val =~ tr/+/ /;
$val =~ s/%([a-fA-f0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-f0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$searchField{$name} = $val;
}
}
return (%searchField);
}