1

I have this form that looks like so in a cgi file:

print "<FORM NAME='LAYOUTFORM' ACTION='Handler.cgi' METHOD=POST>";
print "<table border='0' align=center>\n";
print "<tr><td>Search by:<SELECT ID='Forms Combo Box1' NAME='what_to_do'><OPTION VALUE='title'>Title</OPTION><OPTION VALUE='description'>Description</OPTION><OPTION   VALUE='author'>Author</OPTION></td>";
print "<td><INPUT ID='SearchArea' TYPE=TEXT NAME='searchbox' VALUE='' SIZE=27 MAXLENGTH=100></td>";
print "<td><INPUT TYPE=SUBMIT NAME='searchbutton' VALUE='Search' ID='Form_Search'></td></tr>";
print "</table>\n";
print "</form>";

Then I have this:

#!/usr/local/bin/perl

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

$searchinput = param('searchbox');

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

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

my $sth = $dbh->prepare( q(SELECT * FROM BookStore WHERE bAuthor = $searchinput) ) or 
die( "Cannot prepare statement: ", $dbh->errstr(), "\n" );

my $rc = $sth->execute() or
die( "Cannot execute statement: ", $sth->errstr(), "\n" );

I am getting this error in commandline:

Uknown column '$searchinput' in 'where clause' at Search.cgi line 17.

What I am trying to do is the user would enter a name into the textbox on the main.cgi. Then hit the search button and the search.cgi would retrieve the info of the row that matches in the column of the table.

1 Answer 1

1

Try changing this line:

my $sth = $dbh->prepare( q(SELECT * FROM BookStore WHERE bAuthor = $searchinput) ) or 
die( "Cannot prepare statement: ", $dbh->errstr(), "\n" );

To:

my $query = sprintf ('SELECT * FROM BookStore WHERE bAuthor = %s', 
                     $dbh->quote("$searchinput"));
Sign up to request clarification or add additional context in comments.

2 Comments

That worked wonders and perfectly. Thank you very much will mark as answered.
Your first suggestion is a disaster waiting to happen. Always use placeholders for untrusted user input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.