<?
include ("connection.php");


$name=$_POST["name"];
$comments=$_POST["comments"];
$type=$_POST["type"];
$supplier=$_POST["supplier"];
$regulation=$_POST["regulation"];
$gene=$_POST["gene"];
$seq=$_POST["seq"];
$pmid=$_POST["pmid"];


$requete="SELECT \"NAME\" from \"GENE\" where \"NAME\"='$gene'";
$stmt=pg_query($c,$requete);
if (pg_num_rows($stmt)==0)
	{
	print "<center>Please choose a valid Transcript ID<br></center>";
	include ("frames_footer.php");
	return;
	}



$seq=$_POST["seq"];

$id=$_POST["id"];


if (strlen($name)!=0) $rname="\"NAME\"='$name'";
if (strlen($comments)!=0) $rcomments=", \"COMMENTS\"='$comments'";
if (strlen($type)!=0) $rtype=", \"TYPE\"='$type'";
if (strlen($supplier)!=0) $rsupplier=", \"SUPPLIER\"='$supplier'";
if (strlen($regulation)!=0) $rregulation=", \"REGULATION\"='$regulation'";
if (strlen($gene)!=0) $rgene=", \"ASSOCIATED_GENE_ID\"='$gene'";


pg_query($c,"begin");
$requete="UPDATE \"MOLECULAR_TOOL\" SET $rname $rcomments $rtype $rsupplier $rregulation $rgene where \"MOLECULAR_TOOL_ID\"='$id'";
pg_query($c,$requete) or die(pg_last_error($c));


$oid=pg_lo_create($c);
$requete="UPDATE \"SEQUENCE\" set \"SEQUENCE\"='$oid' where \"MOL_ID\"='$id'";
$stmt=pg_query($c, $requete) or die(pg_last_error($c));
$handler=pg_lo_open($c,$oid,"w");
pg_lo_write($handler,$seq);
pg_lo_close($handler);


$requete="SELECT \"PUBMED_ID\" from \"REFERENCE\" where \"BIO_ID\"='$id'";
$stmt=pg_query($c,$requete);
if (pg_num_rows($stmt)==0)
	{

	if (strlen($pmid)!=0)
		{	
		$requete="INSERT INTO \"REFERENCE\" (\"BIO_ID\",\"PUBMED_ID\") values ('$id','$pmid')";
		pg_query($c,$requete);
		}
	}
	else
	{
		if (strlen($pmid)!=0)
		{
		$requete="UPDATE \"REFERENCE\" SET \"PUBMED_ID\"='$pmid' where \"BIO_ID\"='$id'";
		$stmt=pg_query($c,$requete) or die (pg_last_error($c));
		}
		else
		{
		$requete="UPDATE \"REFERENCE\" SET \"PUBMED_ID\"=NULL where \"BIO_ID\"='$id'";
		pg_query($c,$requete);
		}


	}


pg_query($c,"commit");

?>
<SCRIPT LANGUAGE="JavaScript"> javascript:opener.location.reload(); self.close(); </script>
