#!/usr/bin/perl

use DBI;
use CGI;

$param=new CGI;
$tabulka = $param ->param('table');
$radka = $param ->param('radka');
$id = $param ->param('id');

print "Content-type:text/html\n\n";
&init;
print "<body bgcolor='#ffffff'>";



if ($tabulka&&$radka&&$id)
{
$dbh = DBI->connect("$INIT","$LOGIN","$PASS");
$pop = $dbh->prepare("show columns from $tabulka");
$pop->execute;

$vysl =$radka/1;
if(!$vysl)
{
 $sth=$dbh->prepare("select * from $tabulka where $id like '$radka'") || print $dbh->errstr;
}else
{
 $sth=$dbh->prepare("select * from $tabulka where $id=$radka") || print $dbh->errstr;
}
$sth->execute;
@sloupce = @{$sth->{NAME}};
#@aaa = @{$sth->{TYPE}};

print qq(Selected table : <a href="view.cgi?table=$tabulka"><b>$tabulka</b></a><br>\n);

print qq(<form action="addu.cgi" method="post">
<input type="hidden" name="table11" value="$tabulka">
<input type="hidden" name="radka11" value="$radka">
<input type="hidden" name="id11" value="$id">
);


print qq(<br>
<table border="1" width="100%">
<tr><td bgcolor="#ddddff"  colspan="2"> Submit : <input type="Submit" name="ok" value="ok"></td></tr>
<tr><td bgcolor="#eeeeff"  colspan="2"> columns : <b>@sloupce</b></td></tr>
);

@rr=$sth->fetchrow_array;
$cnt=0;
foreach $ra(@sloupce)
{
 @types=$pop->fetchrow_array;
 print qq(<tr bgcolor='#eeeeee'><td><b> $ra </b></td><td>$types[1]</td></tr>\n);
 if (($types[1]=~/text/)||($types[1]=~/blob/))
 {
  print qq(<tr><td colspan="2"><textarea cols="115" rows="10" name="$ra">$rr[$cnt]</textarea></td></tr>\n);
 }else
 {
  print qq(<tr><td colspan="2"><input type="text" size="115" name="$ra" value="$rr[$cnt]"></td></tr>\n);
 }
 $cnt++;
}

print qq(
<tr><td bgcolor="#ddddff"  colspan="2"> Submit : <input type="Submit" name="ok" value="ok"></td></tr>
</table>
</form>
</body>
);

$sth->finish;
$pop->finish;
$dbh->disconnect;
}

# -------------------------------------------------
sub init
{
 open(F,"dat.conf");
 @o =<F>;
 close(F);
 %COLS=();
 foreach $radek (@o)
 {
  next if $radek=~/^#/; next unless $radek=~/\S/;
  if ($radek=~/^init/i)   {($INIT)=($radek=~/^init\s*=\s*(\S*)/i);};
  if ($radek=~/^login/i)   {($LOGIN)=($radek=~/^login\s*=\s*(\S*)/i);};
  if ($radek=~/^pass/i)   {($PASS)=($radek=~/^pass\s*=\s*(\S*)/i);};
  #if ($radek=~/^tables/i) {($tmp)=($radek=~/^tables\s*=\s*(.*)/i);$tmp=~s/\s*//g;@TABL=split(",",$tmp);};
  #if ($radek=~/^cols/i) {($tmp1,$tmp2)=($radek=~/^cols\s*(.*?)\s*=\s*(.*)/i);$tmp2=~s/\s*//g;$tmp2=~s/,/,/g;$COLS{"$tmp1"}="$tmp2";};
 }
 if(!$INIT) {&err("100","No init string");exit(0);}
}

sub err
{
 print "<br> Error :<b>$_[0]</b> : $_[1]\n";
}