<?
function InsertSp($spid,$fam,$spname,$local,$common,$grade)
{
	include "connect.inc.php";
	$sqlchk = "select * from fishspecies where scientific ='$spname'";
	//echo $sqlchk;
	$qrychk = mysql_query($sqlchk);
	$numchk = mysql_num_rows($qrychk);
	if($numchk < 1)
	{
	$sqlinsert = "insert fishspecies(spid,family,scientific,local,common,grade) values($spid,'$fam','$spname','$local','$common','$grade')";
	//echo $sqlinsert;
	mysql_query($sqlinsert);
	}
	else
	{
	require_once("usr_function.php");
	ErrMsg("Species is duplicate!");
	}
}
function EditSp($spid,$fam,$spname,$local,$common,$grade)
{
	include "connect.inc.php";
	$sqledit = "update fishspecies set family='$fam',scientific='$spname',local='$local',common='$common',grade='$grade' where spid='$spid'";
	//echo $sqledit;
	mysql_query($sqledit) or die(mysql_error());
}
function DeleteSp($spid)
{
	include "connect.inc.php";
	$sqldelete = "delete from fishspecies where spid=$spid";
	//echo $sqldelete;
	mysql_query($sqldelete) or die(mysql_error());
}
function getspeciesid($spname)
{
	include "connect.inc.php";
	$sqlgetspid = "select spid from fishspecies where scientific ='$spname'";
	//echo $sqlgetspid;
	$qrygetspid = mysql_query($sqlgetspid);
	$numgetspid = mysql_num_rows($qrygetspid);
	if($numgetspid != 0)
	{
		$resgetspid = mysql_fetch_array($qrygetspid);
		$spid = $resgetspid["spid"];
		return $spid;
	}	
}
function getspname($spid)
{
	include "connect.inc.php";
	$sqlgetspid = "select scientific from fishspecies where spid ='$spid'";
	//echo $sqlgetspid;
	$qrygetspid = mysql_query($sqlgetspid);
	$numgetspid = mysql_num_rows($qrygetspid);
	if($numgetspid != 0)
	{
		$resgetspid = mysql_fetch_array($qrygetspid);
		$spname = $resgetspid["scientific"];
		return $spname;
	}
}
function getspid()
{
	include "connect.inc.php";
	$sqlgetid = "select max(spid) as spmax from fishspecies";
	$qrygetid = mysql_query($sqlgetid);
	$resultgetid = mysql_fetch_array($qrygetid);
	$maxId = $resultgetid["spmax"];
	$spid = $maxId+1;
	return $spid;
}
function InsertSpPic($spid,$picpath)
{
	include "connect.inc.php";
	$sqlinspic = "update fishspecies set sppic = '$picpath' where spid = $spid";
	//echo $sqlinspic;
	mysql_query($sqlinspic) or die(mysql_error());
}
function DeleteSpFile($spid)
{
	include "connect.inc.php";
	$sqldelfile = "select sppic from fishspecies where spid = $spid";
	$qrydelfile = mysql_query($sqldelfile);
	$resdelfile = mysql_fetch_array($qrydelfile);
	$fname = $resdelfile["sppic"];
	//echo $fname;
	if($fname != "")
	{
		$filepath = "species/".$fname;
		unlink($filepath);
	}
	$updatepicpath = "update fishspecies set sppic='' where spid = $spid";
	//echo "Unlink and ".$updatepicpath;
	mysql_query($updatepicpath);
}
?>