#!/usr/bin/perl
#home.adelphi.edu/~ch21221/z.pl

print "content-type:text/html; charset=utf-8\n\n";
use strict;
use warnings;
use CGI qw/:standard/;

use DBI;

 

my $search = param("input")||"";
my $pwd = param("pwd")||"";
 
my $mfile = "/home/ch21221/public_html/movies.txt";
my $sfile = "/home/ch21221/public_html/streams.txt";
my $afile = "/home/ch21221/public_html/assoclist.txt";

open(my $MOV, '<', $mfile) or die "Could not open file '$mfile' $!";
open(my $STR, '<', $sfile) or die "Could not open file '$sfile' $1";
open(my $ASC, '<', $afile) or die "Could not open file '$afile' $1";

print <<ENDHTML;
<html> <head> <title>Movie Search</title> </head> <body>
 
<a href="movies.txt">movies.txt</a>
<br>
<a href="streams.txt">streams.txt</a>
<br>
<a href="assoclist.txt">assoclist.txt</a>
<br>
    <form method="POST" action="z.pl">
        Enter a search word:<br>
        <input type="text" name="input">
				   <input type="password" name="pwd">
        <br><br>
        <input type="submit" value="OK">
    </form>
ENDHTML

searchIt();


print qq(</body>);
print qq(</html>);


sub searchIt {
 
 
if ($pwd){
  
 
my ($dbh, $sth, $name, , $cost,   $stream, $movie);
  $dbh=DBI->connect('dbi:mysql:pepperdb','pepper',$pwd) ||

die "Error opening database: $DBI::errstr\n";
my $sqlstring = "select s.sname ,s.cost , m.mname from  movies m inner join  associations a " .
   " on m.movieId = a.movieId inner join streams s on s.streamId = a.streamId where m.mname " .
  " = '$search'";
$sth=$dbh->prepare($sqlstring)
 ||
die "Prepare failed: $DBI::errstr\n";
 
$sth->execute() ||

die "Couldn't execute query: $DBI::errstr\n";

while (( $stream, $cost, $movie ) = $sth ->fetchrow_array) {

  print "$movie is available on $stream which costs $cost.   <br/>";

}
$sth->finish();

$dbh->disconnect || die "Failed to disconnect\n";
}
}
