Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8841323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:41:13+00:00 2026-06-14T10:41:13+00:00

I am doing my Perl Assignment for the book database. I am required to

  • 0

I am doing my Perl Assignment for the book database. I am required to make and html form that, where the user will type input such as:

1)title
2)author
3)language
4)year
5)sales

then i need CGI to process it. and store into database ‘books’.

I am using MySql database.

The problem I am facing it is not outputting the values I have entered, I tried many ways debugging it, but still no results.

Here is my html form ‘CreateForm.html”

<form method="post" action="http://localhost:8080/cgi-bin/create.cgi"  >
<table border="1">
<tr>
<td>Title: </td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>Author: </td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>Language: </td>
<td><input type="text" name="language"></td>
</tr>
<tr>
<td>Year of Publication: </td>
<td><input type="text" name="year"></td>
</tr>
<tr>
<td>Estimated sales: </td>
<td><input type="text" name="sales"></td>
</tr>
<tr>
<td><input type="submit" /></td>
</tr>
</table>
</form> 

I am running XAMPP on windows

Here is basically my code ‘create.cgi’

#!"C:\xampp\perl\bin\perl.exe"
print "Content-type: text/html\r\n\r\n";

#include section
use strict;
use DBI;
use CGI;
my $q=new CGI;

#declaration section
my $name;
my $value;
my $sth;
my %FORM;
my $dbh;
my $sql;
my $rv;
my $key;
my $buffer;
my @pairs;
my $pair;
my $title;
my $author;
my $language;
my $year;
my $sales;



if ($ENV{'REQUEST_METHOD'} eq 'POST') {
  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
  $buffer = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
  ($name, $value) = split(/=/, $pair);
  $name =~ tr/+/ /;
  $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $FORM{$name} = $value;
}

#parsing data
$title=$q->param('title');
$author=$q->param('author');
$language=$q->param('language');
$year=$q->param('year');
$sales=$q->param('sales');


#database connectivity
$dbh = DBI->connect('DBI:mysql:books', 'root', ''
               ) || die "Could not connect to database: $DBI::errstr";



#test insert
$sth=$dbh->do('INSERT INTO BOOKS (TITLE,AUTHOR,LANGUAGE,YEAR,SALES) VALUES (
    "Nineteen eight four",
    "George Orwell",
    "English",
    1947,
    25000000)
');


$sql="INSERT INTO BOOKS (TITLE,AUTHOR,LANGUAGE,YEAR,SALES) values ('$title','$author','$language','$year','$sales')";

$sth=$dbh->prepare($sql) or die "can't prepare $sql: $dbh->errstrn";
$rv=$sth->execute;


if ($rv==1){
print "Record has been successfully created !!!<br>";
}else{
print "Error!!while inserting record\n";
exit;
}

$sth = $dbh->prepare("SELECT * FROM BOOKS");
$sth->execute();



foreach $key(sort keys %FORM){
print "<h3>$key: $FORM{$key}</h3>";
}


print "<html>";
print "<head>";
print "<title>Create Form</title>";
print "</head>";
print "<body>";

print "Title: $FORM{'title'}              <br/>";
print "Author: $FORM{'author'}                <br/>";
print "Language: $FORM{'language'}                <br/>";
print "Year: $FORM{'year'}                <br/>";
print "Sales: $FORM{'sales'}                <br/>";


foreach $key(sort keys %FORM){
print "<h3>$key: $FORM{$key}</h3>";
}


#retrieving all tables to check
#while (my $ref = $sth->fetchrow_hashref()) {
#print "<br>Found a row:\n
#          id = $ref->{'BOOKID'}, 
#          tname = $ref->{'TITLE'}, 
#          author: $ref->{AUTHOR}, 
#          language: $ref->{'LANGUAGE'}, 
#          year: $ref->{'YEAR'}, 
#          sales: $ref->{'SALES'} \n";
#}


#while (my $ref = $sth->fetchrow_hashref()) {
#
#          %rec=%{$pRec};
#          print "$rec{'title'}";
#}


print "</body>";
print "</html>";



$sth->finish();
$dbh->disconnect();

the line

foreach $key(sort keys %FORM){
print "<h3>$key: $FORM{$key}</h3>";
}

doesn’t output anything

the output I receive

Record has been successfully created !!!
Title:
Author:
Language:
Year:
Sales:

Empty entries, however the INSERT operation has been performed

30  Nineteen eight four     George Orwell   English     1947    25000000

Thanks in Advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:41:15+00:00Added an answer on June 14, 2026 at 10:41 am

    I don’t understand why you’re messing around with the %FORM hash, considering you are correctly accessing the parameters elsewhere, such as:

    $title=$q->param('title');
    

    Therefore, why don’t you just output $title?

    print "Title: $title <br/>";
    

    If you want a hash with the keys and values of the parameters, then get it into a hashref with:

    $params = $q->Vars;
    

    Then, if you want to iterate over that hashref, do something like this:

    foreach my $key ( sort keys %$params ) {
      print "$key has a value of $params->{$key}\n";
    }
    

    ———————— 8< ————————

    Addendum, not related to the question:

    You really don’t want to put your values in your SQL the way you are doing it:

    $sql="INSERT INTO BOOKS (TITLE,AUTHOR,LANGUAGE,YEAR,SALES) values ('$title','$author','$language','$year','$sales')";
    

    Instead, you want to use placeholders and pass in the parameters to the execute() method:

    $sql = "INSERT INTO BOOKS (TITLE,AUTHOR,LANGUAGE,YEAR,SALES) values (?,?,?,?,?)";
    $sth = $dbh->prepare($sql) or die "can't prepare $sql: $dbh->errstrn";
    $sth->execute( $title, $author, $language, $year, $sales );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a Perl/CGI form that needs to present a variable number (1 -
I know a great thing about perl is that there's multiple ways of doing
Ok this is what I'm doing. I'm making a perl interpreter for documents that
I learning Perl and doing a home made project to my family (a subscription
Replacing the Special Character using Perl while i am doing this . I got
Doing an ajax get request works as expected using the following code: $.ajax({ type:
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
Doing my first tryouts with foreign keys in a mySQL database and are trying
Doing some homework here (second assignment, still extremely green...). The object is to read
I am doing a Perl OO example (mainly to reacquaint myself with Perl) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.