I am currently trying to develope a perl CGI script that will allow a user to enter a name on an HTML form, and it will search the database with that name. Once it finds the matched names, it will display their name and telephone number on the screen. I'm not completely sure how to go about this, so I was wondering if anyone could share any sort of insight.

Thank you very much in advance.


<HEAD><TITLE>Search Telephone Number</TITLE></HEAD>
<H3>Search A Telephone Number</H3>
<FORM ACTION="project2_searchnum.pl" METHOD=POST>
    <TD><INPUT NAME=fullname SIZE="20"></TD>
<P><INPUT TYPE=submit VALUE="Submit"> <INPUT TYPE=reset> <INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;"></P>




#use warnings;
#use strict;

use DBI;
use DBD::mysql;
use CGI qw( :standard );

my $cgi = new CGI;

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

#connecting to the database 
my $dbh = DBI->connect( 
"DBI:mysql:****", "******", "******" ) or
   die( "Could not make connection to database: $DBI::errstr" );

#setting params from the html form
$q=new CGI;

#Webpage creation
print "<HTML>\n";
print "<HEAD><TITLE>Project 1 - Search a Telephone Number</TITLE><BASEFONT SIZE=5></HEAD>\n";
print "<H1>Search a Telephone Number</H1>\n";

#Display names and numbers from search here

print "<a href='project2_main.html'><INPUT TYPE='button' VALUE='Main Menu'</a>\n";

print "</BODY>\n";
print "</HTML>\n";


If you've named your table phonelist and your columns name and number, insert this at line 23:

my $sth = $dbh->prepare("SELECT name,number from phonelist where name=?");
my @row;

At line 33, insert this:

while (@row = $sth->fetchrow_array)
  {print "Found:  Name=$row[0] Phone Number=$row[1]<br>\n";
This article has been dead for over six months. Start a new discussion instead.