#!/usr/bin/perl

use CGI;
use DBI;

$query=new CGI;
$address  = $query->param('address')  || 'dewey';
$database = $query->param('database') || 'mysql';
$username = $query->param('username') || 'bastille';
$password = $query->param('password') || '';
$table    = $query->param('table')    || 'user';
$select   = $query->param('select')   || 'select * from '.$table;
if ($password eq '') { $pwstring = ''; } else { $pwstring = '-p=$password' };

print $query->header;
print "<HTML><head>\n";
print "<title>Table $table Contents</title>\n";
print "<META HTTP-EQUIV='EXPIRES' CONTENT='0'>\n";
print "<META HTTP-EQUIV='Pragma' CONTENT='no-cache'>\n";
print "</head><body>\n";

#load the driver for mysql
$drh=DBI->install_driver('mysql') || cgidie("Could not load mysql driver\n");

$dbh=$drh->connect("$database:$address","$username","$password") || cgidie("Failed to connect to mysql server\n");

$cursor=$dbh->prepare("describe $table") || cgidie("Describe failed: describe $table");
$cursor->execute;
@primarykeys = ();
@fields = ();
while (@fielddesc = $cursor->fetchrow) {
  push @fields, $fielddesc[0];
  if ($fielddesc[3] eq 'PRI') {
    push @primarykeys, $fielddesc[0];
    $primarykeys{$fielddesc[0]} = 1;
  }
}
@fieldnames = @fields;

print <<EOF
<h1>Table $table Contents</h1>
<a href='mysql.html'>$address:$username</a> :
<a href='startup.pl?address=$address&username=$username&password=&password'>$database</a> :
<a href='use_database.pl?address=$address&database=$database&username=$username&password=&password'>$table</a> :
<table border=0>
  <tr>
    <form method=post action='use_table.pl'>
    <td>
      <input name=address value='$address' type=hidden>
      <input name=database value='$database' type=hidden>
      <input name=username value='$username' type=hidden>
      <input name=password value='$password' type=hidden>
      <input name=table value='$table' type=hidden>
      Enter new select statement:<BR>
      Note:  Use ""s when entering strings into your SQL command.
    </td><td>
      <input name=select value='$select' type=text maxlength=255 size=50>
    </td><td>
      <input type=submit value='Execute'>
    </td>
    </form>
  </tr><tr>
    <td>
      <a href='insert_rec.pl?address=$address&database=$database&username=$username&password=$password&table=$table&select=$select'>Insert New Record</a>
    </td>
  </tr>
</table>
<table border=1 cellpadding=0 cellspacing=0>
<tr>
EOF
;

@second_line = ();
$top = $#fields % 2;
while ($fieldname = shift @fields) {
  print "<th colspan=4>$fieldname</th>\n";
  $second_line[++$#second_line] = shift @fields;
}
if ($top) { print "<th colspan=2 align=right>Delete</th></tr>\n"; }
print "</tr><tr><th colspan=2 align=left>Edit</th>\n";
while ($fieldname = shift @second_line) {
  print "<th colspan=4>$fieldname</th>\n";
}
if (!$top) { print "<th colspan=2 align=right>Delete</th></tr>\n"; }

$cursor=$dbh->prepare($select) || cgidie("Prepare failed: $select");
$cursor->execute || cgidie("Select failed: $select\n".$cursor->errstr);
while (@record = $cursor->fetchrow) {
  $select_str = '';
  $table_str = '';
  for ($x = 0; $x < $#record + 1; $x ++) {
    $field = $record[$x];
    if ($select_str eq '') {
      $select_str = "address=$address&database=$database&username=$username&password=$password&table=$table&select=$select";
    }
    if (($#primarykeys == -1) || (exists $primarykeys{$fieldnames[$x]})) {
      $select_str .= "&field_$fieldnames[$x]=$field";
    }
    $table_str .= "<td colspan=2>$field</td>\n";
  }
  print "<tr><td><a href='mod_record.pl?$select_str'><img src='ball.green.gif' border=0></a></td>\n";
  print $table_str;
  print "<td align=right><a href='del_record.pl?$select_str'><img src='ball.purple.gif' border=0></a></td></tr>\n";
}
$cursor->finish;

print <<EOF
</table>
<hr>
<address>
Orignial Code by: <a href='mailto:tps\@users.buoy.com'>Tim Sailer</a><BR>
Modified by: <a href='mailto:hightide\@iname.com'>Sean Bastille</a>
</address>
</body>
</html>
EOF
;

sub cgidie {
  ($die_str) = @_;
print <<EOF
CGI_Error:<p><pre>
$die_str
</pre>
</body>
</HTML>
EOF
;
  die $dir_str;
}
      
