#!perl
use DBI;
use CGI;
print "Content-Type: text/html\n\n";
$head = <<'HEAD';
Branch Inventory Query
div>
BODY
$results = <<'RESULTS';
RESULTS
$i = 0;
$c = new CGI();
$searchTerm = $c->param("searchTerm");
$likeEquals = $c->param("likeEquals");
$field = $c->param("field");
$searchTerm =~ s/'/\\'/;
$likeEquals =~ s/'/\\'/;
$field =~ s/'/\\'/;
if($searchTerm eq "") {
$searchTerm = "void";
}
$h = DBI->connect("dbi:mysql:inventory:localhost","ID","PW") or die("couldnt connect to database");
$query = "SELECT SerialNumber, AssetTag, AssetType, Branch, Status, Comments FROM assets WHERE $field $likeEquals ? ORDER BY AssetType";
$q = $h->prepare($query);
$rows = $q->execute($searchTerm);
%columns = (
SerialNumber => "Serial Number",
AssetTag => "Asset Tag",
AssetType => "Asset Type",
Branch => "Branch",
Status => "Status",
Comments => "Comments",
);
if($rows > 0) {
$results .= "| Your search for $searchTerm in $columns{$field} returned $rows result(s). |
";
$results .= "";
foreach $field (@{$q->{NAME}}) {
$results .= "| $columns{$field} | ";
}
$results .= "
";
if($c->param("searchTerm")) {
while ($row = $q->fetchrow_hashref) {
if($i % 2 == 0) {
$results .= "| $row->{SerialNumber} | $row->{AssetTag} | $row->{AssetType} | $row->{Branch} | $row->{Status} | $row->{Comments} |
";
} else {
$results .= "| $row->{SerialNumber} | $row->{AssetTag} | $row->{AssetType} | $row->{Branch} | $row->{Status} | $row->{Comments} |
";
}
$i++;
}
}
} else {
$results .= "No results matched your Inventory query.";
}
$h->disconnect();
$results .= <<'RESULTS';
RESULTS
print $head;
print $body;
print $results;
print $tail;