<?php
if(!isset($_SESSION['guest']) && !$_SESSION['guest']  && ($_SESSION['loggedin'] || isset($_SESSION['loggedin']))) {
$Current_Date = date('Y-m-d H:i:s');  
include("addslashes_sptoolcollection.php");

if($formSubmitSave)
{
/*-- Select year from Collection Date ----------------#
#                                                     #
#                                                     #
#                                                     #
#-----------------------------------------------------*/

for ($i = 1500; $i < 2100; $i++) {
if(substr($sqlCollectionDateMin, 0, -6) == $i)
{$CollectionYear = $i; }}

/*-- End of "Select year from Collection Date" -------#
#                                                     #
#                                                     #
#                                                     #
#-----------------------------------------------------*/

if($radioPreferred == 'taxa1')
  {$PreferredFlag1 = '1'; }

  if($radioPreferred != 'taxa1')
  {$PreferredFlag1 = '0'; }

  if($radioPreferred == 'taxa2')
  {$PreferredFlag2 = '1'; }
  
  if($radioPreferred != 'taxa2')
  {$PreferredFlag2 = '0'; }
  
  if($radioPreferred == 'taxa3')
  {$PreferredFlag3 = '1'; }
  
  if($radioPreferred != 'taxa3')
  {$PreferredFlag3 = '0'; }  
      
  if($radioPreferred == 'taxa4')
  {$PreferredFlag4 = '1'; }
  
  if($radioPreferred != 'taxa4')
  {$PreferredFlag4 = '0'; }  

  
$sql1 = "UPDATE sptoolcollection SET UnitID_Specimen = '$sqlSpecimenNumber', CollectionCode_Specimen = '$sqlCollectionCode', InstitutionCode_Specimen = '$sqlInstitutionCode', 
ExpeditionDetails = '$sqlExpedition', FK_BiogeographicRegion = $formBiogeoRegion, FK_Country = $formCountry, FK_SeasAndOceans = $formOcean, Region = '$sqlRegion', 
Locality = '$sqlLocality', Locality_ID = '$sqlLocalityID', Habitat = '$sqlHabitat', FK_SoilTexture = $formSoilTexture, FK_SoilpH = $formSoilpH, FK_HabitatCode = $formHabitatCode, 
FK_LandUseCode = $formLandUseCode, FK_Slope = $formSlope, FK_Aspect = $formAspect, DepthAccuracyStatement = '$sqlDepthAccuracy', DepthMin = $sqlDepthMin, DepthMax = $sqlDepthMax, 
DepthUnit = '$sqlDepthUnit', DepthMethod = '$sqlDepthMethod', DepthRaw = '$sqlDepthRaw', GeodeticDatum = '$sqlGeoDatum', FK_GeocodeMethod = $formGeoMethod, 
CoordinatesAccuracyStatement = '$sqlCoordinatesAccuracy', CoordinatesErrorDistance = '$sqlErrorDistance', LongitudeMin = $sqlLongitudeMin, LongitudeMax = $sqlLongitudeMax, 
LatitudeMin = $sqlLatitudeMin, LatitudeMax = $sqlLatitudeMax, LongitudeRaw = '$sqlLongitudeRaw', LatitudeRaw = '$sqlLatitudeRaw', 
AltitudeAccuracyStatement = '$sqlAltitudeAccuracy', AltitudeMin = $sqlAltitudeMin, AltitudeMax = $sqlAltitudeMax, AltitudeUnit = '$sqlAltitudeUnit', 
AltitudeMethod = '$sqlAltitudeMethod', AltitudeRaw = '$sqlAltitudeRaw', CollectionDateMin = $sqlCollectionDateMin, CollectionDateMax = $sqlCollectionDateMax, 
CollectionYear = '$CollectionYear', Collectors = '$sqlCollector', CollectorsNo = '$sqlCollectorsNo', CollectionNotes = '$sqlCollectionNotes', 
CollectionMethod = '$sqlCollectionMethod', FK_Sex = $formSex, FK_BasisOfRecord = $formBasisOfRecord, FK_KindOfUnit = $formKindOfUnit, FK_PrepType = $formPrepType, 
PrepDate = $sqlPrepDate, FK_PrepStaff = $formPrepStaff, PrepMaterials = '$sqlPrepMaterials', PrepProcess = '$sqlPrepProcess', FK_AcquiredFrom = $formAcquiredFrom, 
EscapedCoordinates = '$radioProtect', Permission = '$radioPermission', Blocked_For_Loaning = $formBlockLoan, Blocked_For_Publishing = $formBlockPublish, 
Blocked_Until = $sqlBlockDate, Loan_Conditions = '$sqlLoanConditions', FK_Type = $formType, TypeDesignation = '$sqlTypeDesignation', 
Notes = '$sqlNotes', Notes_Internal = '$sqlNotesInternal', AccessionNo = '$sqlAccNo', AccessionName = '$sqlAccName', LifeStage = '$sqlLifeStage', LifeForm = '$sqlLifeForm', FK_Strat_Series = $formSeries, 
Qualifier_Series = '$sqlQualifierSeries', FK_Strat_Stage = $formStage, Qualifier_Stage = '$sqlQualifierStage', FossilZone = '$sqlFossilZone', Formation = '$sqlFormation', 
SectionDetails = '$sqlSectionDetails', SpecimenDetails = '$sqlSpecimenDetails', FK_ParentID_Collection = $formParentFK, FK_RelationToParent = $formRelation, Updated_Who = '$log', Updated_When = '$Current_Date' WHERE ID_Collection = '$ID_Collection'";

$result1 = mysqli_query($GLOBALS["___mysqli_ston"], $sql1);

#######################################Error 1###############################################################

if(!$result1)
{ $msg = $sql1."\n";
  $msg .= "####Error 1####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  include ("../config/Errorreport.php");   
  }
  
############################################################################################################# 

  if($result1)
  {
  
  $NameAuthorYear1 = $formGenus1;
  if($formSubgenus1) { $NameAuthorYear1 .= " (".$formSubgenus1.")"; }
  if($formSpecificEpithet1) { $NameAuthorYear1 .= " ".$formSpecificEpithet1; }
  if($formRank1) { $NameAuthorYear1 .= " ".$formRank1; }
  if($formInfraspecificEpithet1) { $NameAuthorYear1 .= " ".$formInfraspecificEpithet1; }
  if($formAuthorParenth1) { $NameAuthorYear1 .= " (".$formAuthorParenth1; }
  if($formAuthorYearParenth1) { $NameAuthorYear1 .= ", ".$formAuthorYearParenth1; }
  if($formAuthorParenth1) { $NameAuthorYear1 .= ")"; }
  if($formAuthor1) { $NameAuthorYear1 .= " ".$formAuthor1; }
  if($formAuthorYear1) { $NameAuthorYear1 .= ", ".$formAuthorYear1; }
  
  $sqlID_TaxonIdentified = $_POST['Genus1'];
  $sqlGenus1 = addslashes($formGenus1);
  $sqlSubgenus1 = addslashes($formSubgenus1);
  $sqlSpecificEpithet1 = addslashes($formSpecificEpithet1);
  $sqlInfraspecificEpithet1 = addslashes($formInfraspecificEpithet1);  
  $sqlAuthor1 = addslashes($formAuthor1);
  $sqlAuthorYear1 = addslashes($formAuthorYear1);
  $sqlAuthorParenth1 = addslashes($formAuthorParenth1);
  $sqlAuthorYearParenth1 = addslashes($formAuthorYearParenth1);
  $sqlNameAuthorYear1 = addslashes($NameAuthorYear1);
  $sqlIdentifier1 = addslashes($formIdentifier1);
  $sqlIdentDate1 = addslashes($formIdentificationDate1);
  $sqlIdentNotes1 = addslashes($formIdentNotes1);      
  
  
  $sql2 = "UPDATE sptooltaxa  SET Genus = '$sqlGenus1', Subgenus = '$sqlSubgenus1', SpecificEpithet = '$sqlSpecificEpithet1', AuthorParenth = '$sqlAuthorParenth1', YearParenth = '$sqlAuthorYearParenth1', Author = '$sqlAuthor1', Year = '$sqlAuthorYear1', Rank = '$formRank1', InfraspecificEpithet = '$sqlInfraspecificEpithet1', NameAuthorYear = '$sqlNameAuthorYear1', KindOfIdentification = '$formIdentType1', Identifier = '$sqlIdentifier1', IdentificationDate = '$sqlIdentDate1', PreferredFlag = '$PreferredFlag1', IdentificationNotes = '$sqlIdentNotes1', Updated_Who = '$log', Updated_When = '$Current_Date' WHERE FK_Collection = '$ID_Collection' AND ID_TaxonIdentified = '$sqlID_TaxonIdentified'";
  
  $result2 = mysqli_query($GLOBALS["___mysqli_ston"], $sql2);
  
  
  if($sqlID_TaxonIdentified == "" and isset($sqlID_TaxonIdentified)) {
   $sql2 = "INSERT INTO sptooltaxa (FK_Collection, Genus, Subgenus, SpecificEpithet, AuthorParenth, YearParenth, Author, Year, Rank, InfraspecificEpithet, NameAuthorYear, KindOfIdentification, Identifier, IdentificationDate, PreferredFlag, IdentificationNotes, Created_Who)" .
                            "VALUES ('$ID_Collection','$sqlGenus1','$sqlSubgenus1','$sqlSpecificEpithet1','$sqlAuthorParenth1','$sqlAuthorYearParenth1','$sqlAuthor1','$sqlAuthorYear1','$formRank1','$sqlInfraspecificEpithet1','$sqlNameAuthorYear1','$formIdentType1','$sqlIdentifier1','$sqlIdentDate1','$PreferredFlag1','$sqlIdentNotes1','$log')"; 
  $result2 = mysqli_query($GLOBALS["___mysqli_ston"], $sql2); 
  $sqlID_TaxonIdentified = ((is_null($___mysqli_res = mysqli_insert_id($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
  }
  
  if($result2) {
  
   $sql3a = "SELECT Created_When, Created_Who FROM sptoolhighertaxon WHERE FK_TaxonIdentified = '$sqlID_TaxonIdentified'";
  $result3a = mysqli_query($GLOBALS["___mysqli_ston"], $sql3a);
  while($row = mysqli_fetch_object($result3a)) 
  { $Created_When = $row->Created_When;
    $Created_Who = $row->Created_Who; }
   
   
   $sql3b = "DELETE FROM sptoolhighertaxon WHERE FK_TaxonIdentified = '$sqlID_TaxonIdentified'";
   $result3b = mysqli_query($GLOBALS["___mysqli_ston"], $sql3b);
  
  if($_POST['formHigherTaxon1'] != "") {
  foreach($_POST['formHigherTaxon1'] as $index2 => $HigherTaxaFree1)
    {
    if(!empty($HigherTaxaFree1)) {
    $Rank1 = $_POST['hiddenHigherTaxon1'][$index2];
    
    $sql4 = "INSERT INTO sptoolhighertaxon (FK_TaxonIdentified, HigherTaxon, TaxonRank, Created_Who, Created_When, Updated_Who, Updated_When) " .
                        "VALUES ('$sqlID_TaxonIdentified','$HigherTaxaFree1','$Rank1','$Created_Who','$Created_When','$log','$Current_Date')";
    $result4 = mysqli_query($GLOBALS["___mysqli_ston"], $sql4);
                        
    }
    }
  }
  }
  if($_POST['FurtherID_Taxa'] != "") {
  foreach($_POST['FurtherID_Taxa'] as $index => $FurtherIdent)
  {
  
     $formGenusFurtherIdent =  $_POST['formGenus'][$FurtherIdent];
     $formSubgenusFurtherIdent =  $_POST['formSubgenus'][$FurtherIdent];
     $formSpecificEpithetFurtherIdent =  $_POST['formSpecificEpithet'][$FurtherIdent];
     $formRankFurtherIdent =  $_POST['formRank'][$FurtherIdent];
     $formInfraspecificEpithetFurtherIdent =  $_POST['formInfraspecificEpithet'][$FurtherIdent];
     $formAuthorParenthFurtherIdent =  $_POST['formAuthorParenth'][$FurtherIdent];
     $formAuthorYearParenthFurtherIdent =  $_POST['formAuthorYearParenth'][$FurtherIdent];
     $formAuthorFurtherIdent =  $_POST['formAuthor'][$FurtherIdent];
     $formAuthorYearFurtherIdent =  $_POST['formAuthorYear'][$FurtherIdent];
	 if($_POST['radioPreferred'] == $FurtherIdent) {
     $PreferredFlag = '1'; }
	 else {$PreferredFlag = '0'; }
     $formIdentifierFurtherIdent = $_POST['formIdentifier'][$FurthIdent];
     $formIdentDateFurtherIdent = $_POST['formIdentificationDate'][$FurtherIdent];
     $formIdentNotesFurtherIdent = $_POST['formIdentNotes'][$FurtherIdent];  
     $formIdentTypeFurtherIdent = $_POST['formIdentType'][$FurtherIdent];
     
     $HigherTaxKingdom = $_POST['formHigherTaxonA'][$FurtherIdent];
     $HigherTaxPhylum = $_POST['formHigherTaxonB'][$FurtherIdent];
     $HigherTaxClass = $_POST['formHigherTaxonC'][$FurtherIdent];
     $HigherTaxOrder = $_POST['formHigherTaxonD'][$FurtherIdent];
     $HigherTaxFamily = $_POST['formHigherTaxonE'][$FurtherIdent];
     $HigherTaxSubfamily = $_POST['formHigherTaxonF'][$FurtherIdent];
     
     $NameAuthorYearFurtherIdent = $formGenusFurtherIdent;
  if($formSubgenusFurtherIdent) { $NameAuthorYearFurtherIdent .= " (".$formSubgenusFurtherIdent.")"; }
  if($formSpecificEpithetFurtherIdent) { $NameAuthorYearFurtherIdent .= " ".$formSpecificEpithetFurtherIdent; }
  if($formRankFurtherIdent) { $NameAuthorYearFurtherIdent .= " ".$formRankFurtherIdent; }
  if($formInfraspecificEpithetFurtherIdent) { $NameAuthorYearFurtherIdent .= " ".$formInfraspecificEpithetFurtherIdent; }
  if($formAuthorParenthFurtherIdent) { $NameAuthorYearFurtherIdent .= " (".$formAuthorParenthFurtherIdent; }
  if($formAuthorYearParenthFurtherIdent) { $NameAuthorYearFurtherIdent .= ", ".$formAuthorYearParenthFurtherIdent; }
  if($formAuthorParenthFurtherIdent) { $NameAuthorYearFurtherIdent .= ")"; }
  if($formAuthorFurtherIdent) { $NameAuthorYearFurtherIdent .= " ".$formAuthorFurtherIdent; }
  if($formAuthorYearFurtherIdent) { $NameAuthorYearFurtherIdent .= ", ".$formAuthorYearFurtherIdent; }
  
  
  $sqlGenusFurtherIdent = addslashes($formGenusFurtherIdent);
  $sqlSubgenusFurtherIdent = addslashes($formSubgenusFurtherIdent);
  $sqlSpecificEpithetFurtherIdent = addslashes($formSpecificEpithetFurtherIdent);
  $sqlInfraspecificEpithetFurtherIdent = addslashes($formInfraspecificEpithetFurtherIdent);  
  $sqlAuthorFurtherIdent = addslashes($formAuthorFurtherIdent);
  $sqlAuthorYearFurtherIdent = addslashes($formAuthorYearFurtherIdent);
  $sqlAuthorParenthFurtherIdent = addslashes($formAuthorParenthFurtherIdent);
  $sqlAuthorYearParenthFurtherIdent = addslashes($formAuthorYearParenthFurtherIdent);
  $sqlNameAuthorYearFurtherIdent = addslashes($NameAuthorYearFurtherIdent);
  $sqlIdentifierFurtherIdent = addslashes($formIdentifierFurtherIdent);
  $sqlIdentDateFurtherIdent = addslashes($formIdentDateFurtherIdent);
  $sqlIdentNotesFurtherIdent = addslashes($formIdentNotesFurtherIdent);  
  
  
  $sql3 = "UPDATE sptooltaxa  SET Genus = '$sqlGenusFurtherIdent', Subgenus = '$sqlSubgenusFurtherIdent', SpecificEpithet = '$sqlSpecificEpithetFurtherIdent', AuthorParenth = '$sqlAuthorParenthFurtherIdent', YearParenth = '$sqlAuthorYearParenthFurtherIdent', Author = '$sqlAuthorFurtherIdent', Year = '$sqlAuthorYearFurtherIdent', Rank = '$formRankFurtherIdent', InfraspecificEpithet = '$sqlInfraspecificEpithetFurtherIdent', NameAuthorYear = '$sqlNameAuthorYearFurtherIdent', KindOfIdentification = '$formIdentTypeFurtherIdent', Identifier = '$sqlIdentifierFurtherIdent', IdentificationDate = '$sqlIdentDateFurtherIdent', IdentificationNotes = '$sqlIdentNotesFurtherIdent', PreferredFlag = '$PreferredFlag', Updated_Who = '$log', Updated_When = '$Current_Date' WHERE FK_Collection = '$ID_Collection' AND ID_TaxonIdentified = '$FurtherIdent'";
  $result3 = mysqli_query($GLOBALS["___mysqli_ston"], $sql3); 
  
  $sql4a = "SELECT Created_When, Created_Who FROM sptoolhighertaxon WHERE FK_TaxonIdentified = '$FurtherIdent'";
  $result4a = mysqli_query($GLOBALS["___mysqli_ston"], $sql4a);
  while($row = mysqli_fetch_object($result4a)) 
  { $Created_When = $row->Created_When;
    $Created_Who = $row->Created_Who; }
  
  $sql4 = "DELETE FROM sptoolhighertaxon WHERE FK_TaxonIdentified = '$FurtherIdent'";
  $result4 = mysqli_query($GLOBALS["___mysqli_ston"], $sql4);
  
  if($HigherTaxKingdom != "") {
  $sql5 = "INSERT INTO sptoolhighertaxon (FK_TaxonIdentified, HigherTaxon, TaxonRank, Updated_Who,Updated_When, Created_When, Created_Who) " .
                        "VALUES ('$FurtherIdent','$HigherTaxKingdom','regnum','$log','$Current_Date','$Created_When','$Created_Who')";
  $result5 = mysqli_query($GLOBALS["___mysqli_ston"], $sql5);}
  
   if($HigherTaxPhylum != "") {
  $sql5 = "INSERT INTO sptoolhighertaxon (FK_TaxonIdentified, HigherTaxon, TaxonRank, Updated_Who, Updated_When, Created_When, Created_Who) " .
                        "VALUES ('$FurtherIdent','$HigherTaxPhylum','phylum','$log','$Current_Date','$Created_When','$Created_Who')";
  $result5 = mysqli_query($GLOBALS["___mysqli_ston"], $sql5);}
  
   if($HigherTaxClass != "") {
  $sql5 = "INSERT INTO sptoolhighertaxon (FK_TaxonIdentified, HigherTaxon, TaxonRank, Updated_Who, Updated_When, Created_When, Created_Who) " .
                        "VALUES ('$FurtherIdent','$HigherTaxClass','classis','$log','$Current_Date','$Created_When','$Created_Who')";
  $result5 = mysqli_query($GLOBALS["___mysqli_ston"], $sql5);}  
  
   if($HigherTaxOrder != "") {
  $sql5 = "INSERT INTO sptoolhighertaxon (FK_TaxonIdentified, HigherTaxon, TaxonRank, Updated_Who, Updated_When) " .
                        "VALUES ('$FurtherIdent','$HigherTaxOrder','ordo','$log','$Current_Date','$Created_When','$Created_Who')";
  $result5 = mysqli_query($GLOBALS["___mysqli_ston"], $sql5);}
  
   if($HigherTaxFamily != "") {
  $sql5 = "INSERT INTO sptoolhighertaxon (FK_TaxonIdentified, HigherTaxon, TaxonRank, Updated_Who, Updated_When, Created_When, Created_Who) " .
                        "VALUES ('$FurtherIdent','$HigherTaxFamily','familia','$log','$Current_Date','$Created_When','$Created_Who')";
  $result5 = mysqli_query($GLOBALS["___mysqli_ston"], $sql5);}
  
   if($HigherTaxSubfamily != "") {
  $sql5 = "INSERT INTO sptoolhighertaxon (FK_TaxonIdentified, HigherTaxon, TaxonRank, Updated_Who, Updated_When, Created_When, Created_Who) " .
                        "VALUES ('$FurtherIdent','$HigherTaxSubfamily','subfamilia','$log','$Current_Date','$Created_When','$Created_Who')";
  $result5 = mysqli_query($GLOBALS["___mysqli_ston"], $sql5);}
  
  }}
  
  $IDsptoolCollection = $ID_Collection;
    include("addfurtheridentifications.php");
    
    //********************Start Ecological parameters**********************
  
  $testecology = "SELECT * FROM sptoolmeasurementorfact WHERE FK_Collection = '$ID_Collection' AND Rank = 'Ecology'";
  $resulttestecology = mysqli_query($GLOBALS["___mysqli_ston"], $testecology);
  if($resulttestecology) {
  while($row = mysqli_fetch_object($resulttestecology)) {
  $EcoCreated_Who = $row->Created_Who;
  $EcoCreated_When = $row->Created_When; } }
    
  $deleteecology = "DELETE FROM sptoolmeasurementorfact WHERE FK_Collection = '$ID_Collection' AND Rank = 'Ecology'";
  $resultdeleteecology = mysqli_query($GLOBALS["___mysqli_ston"], $deleteecology);
 
 if($_POST['formValue1'] != "") {
 
    foreach ($_POST['formValue1'] as $index1 => $Value1)
{
  $Unit1 = $_POST['formUnit1'][$index1];
  $Parameter1 = $_POST['formParameter1'][$index1];
  $Method1 = $_POST['formMethod1'][$index1];

  
      if($Value1=="")
     { unset($Value1);
       unset($Unit1); 
       unset($Parameter1); 
       unset($Method1); }
       
     else {
     
     $sqlEcology1 = "INSERT INTO sptoolmeasurementorfact (FK_Collection, Value, Unit, Parameter, Method, Rank, Created_Who, Created_When, Updated_Who, Updated_When) " .
                        "VALUES ('$ID_Collection','$Value1','$Unit1','$Parameter1','$Method1','Ecology','$EcoCreated_Who','$EcoCreated_When','$log','$Current_Date')";
     $resultEcology1 = mysqli_query($GLOBALS["___mysqli_ston"], $sqlEcology1);
     
#######################################Error sqlEcology###############################################################

if(!$resultEcology1)
{ $msg = $sqlEcology1."\n";
  $msg .= "####Error sqlEcology####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  include ("../config/Errorreport.php");   }
  
#############################################################################################################       
     
     } 
   }  // foreach ($_POST['formValue'] as $index => $Value)
}
 
    
    foreach ($_POST['formValue'] as $index => $Value)
{
  $Unit = $_POST['formUnit'][$index];
  $Parameter = $_POST['formParameter'][$index];
  $Method = $_POST['formMethod'][$index];

  
      if($Value=="")
     { unset($Value);
       unset($Unit); 
       unset($Parameter); 
       unset($Method); }
       
     else {
     
     $sqlEcology = "INSERT INTO sptoolmeasurementorfact (FK_Collection, Value, Unit, Parameter, Method, Rank, Created_Who, Created_When, Updated_Who, Updated_When) " .
                        "VALUES ('$ID_Collection','$Value','$Unit','$Parameter','$Method','Ecology','$EcoCreated_Who','$EcoCreated_When','$log','$Current_Date')";
     $resultEcology = mysqli_query($GLOBALS["___mysqli_ston"], $sqlEcology);
     
#######################################Error sqlEcology###############################################################

if(!$resultEcology)
{ $msg = $sqlEcology."\n";
  $msg .= "####Error sqlEcology####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  include ("../config/Errorreport.php");   }
  
#############################################################################################################       
     
     } 
   }  // foreach ($_POST['formValue'] as $index => $Value)
   
    //********************End Ecological parameters**********************

    //********************Start Morphological parameters**********************
  
  $testmorphology = "SELECT * FROM sptoolmeasurementorfact WHERE FK_Collection = '$ID_Collection' AND Rank = 'Morphology'";
  $resulttestmorphology = mysqli_query($GLOBALS["___mysqli_ston"], $testmorphology);
  if($resulttestmorphology) {
  while($row = mysqli_fetch_object($resulttestmorphology)) {
  $MorphoCreated_Who = $row->Created_Who;
  $MorphoCreated_When = $row->Created_When; } }
    
  $deletemorphology = "DELETE FROM sptoolmeasurementorfact WHERE FK_Collection = '$ID_Collection' AND Rank = 'Morphology'";
  $resultdeletemorphology = mysqli_query($GLOBALS["___mysqli_ston"], $deletemorphology);
 
 if($_POST['formValueM1'] != "") {
 
    foreach ($_POST['formValueM1'] as $index1 => $Value1)
{
  $Unit1 = $_POST['formUnitM1'][$index1];
  $Parameter1 = $_POST['formParameterM1'][$index1];
  $Method1 = $_POST['formMethodM1'][$index1];

  
      if($Value1=="")
     { unset($Value1);
       unset($Unit1); 
       unset($Parameter1); 
       unset($Method1); }
       
     else {
     
     $sqlMorphology1 = "INSERT INTO sptoolmeasurementorfact (FK_Collection, Value, Unit, Parameter, Method, Rank, Created_Who, Created_When, Updated_Who, Updated_When) " .
                        "VALUES ('$ID_Collection','$Value1','$Unit1','$Parameter1','$Method1','Morphology','$EcoCreated_Who','$EcoCreated_When','$log','$Current_Date')";
     $resultMorphology1 = mysqli_query($GLOBALS["___mysqli_ston"], $sqlMorphology1);
     
#######################################Error sqlMorphology###############################################################

if(!$resultMorphology1)
{ $msg = $sqlMorphology1."\n";
  $msg .= "####Error sqlMorphology####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  include ("../config/Errorreport.php");   }
  
#############################################################################################################       
     
     } 
   }  // foreach ($_POST['formValueM'] as $index => $Value)
}
 
    
    foreach ($_POST['formValueM'] as $index => $Value)
{
  $Unit = $_POST['formUnitM'][$index];
  $Parameter = $_POST['formParameterM'][$index];
  $Method = $_POST['formMethodM'][$index];

  
      if($Value=="")
     { unset($Value);
       unset($Unit); 
       unset($Parameter); 
       unset($Method); }
       
     else {
     
     $sqlMorphology = "INSERT INTO sptoolmeasurementorfact (FK_Collection, Value, Unit, Parameter, Method, Rank, Created_Who, Created_When, Updated_Who, Updated_When) " .
                        "VALUES ('$ID_Collection','$Value','$Unit','$Parameter','$Method','Morphology','$EcoCreated_Who','$EcoCreated_When','$log','$Current_Date')";
     $resultMorphology = mysqli_query($GLOBALS["___mysqli_ston"], $sqlMorphology);
     
#######################################Error sqlMorphology###############################################################

if(!$resultMorphology)
{ $msg = $sqlMorphology."\n";
  $msg .= "####Error sqlMorphology####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  include ("../config/Errorreport.php");   }
  
#############################################################################################################       
     
     } 
   }  // foreach ($_POST['formValue'] as $index => $Value)
   
    //********************End Morphological parameters**********************

    
    //********************Start Vouche References**************************
    
 if($_POST['ID_VoucherRef'] != "") { 
  foreach ($_POST['ID_VoucherRef'] as $index => $ID_VoucherRef)
  {
  $VoucherRefText = addslashes($_POST['formVoucherRefText'][$index]);
  $VoucherRefDetail = addslashes($_POST['formVoucherRefDetail'][$index]);
  
  $sql = "SELECT * from reference WHERE ReferenceText = '$VoucherRefText'";
  $result = mysqli_query($GLOBALS["___mysqli_ston"], $sql);
  while($row = mysqli_fetch_object($result))
  {$ID_Ref = $row->ID_References; }
  
  if(!isset($ID_Ref) or $ID_Ref == '0')
  { echo "<span class='error'>Reference not found. Please add it to the system using the green plus symbol.</span>"; }
  else {
  $updateVoucherRef = "UPDATE sptoolreferences SET FK_References = '$ID_Ref', ReferenceDetail = '$VoucherRefDetail', Updated_Who = '$log', Updated_When = '$Current_Date' WHERE ID_Reference = '$ID_VoucherRef'";
  $resultupdateVoucherRef = mysqli_query($GLOBALS["___mysqli_ston"], $updateVoucherRef); 
 
 if(!$resultupdateVoucherRef)
{ $msg = $updateVoucherRef."\n";
  $msg .= "####Error updateVoucherRef####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  include ("../config/Errorreport.php");   } }
  
#############################################################################################################       
     
     } 
   }  // foreach ($_POST['ID_VoucherRef'] as $index => $ID_VoucherRef)
   
   
  
   include("addvoucherref.php");
    
    //********************End Voucher References****************************	
	
	
	
    //********************Start Multimedia Data**************************
    
 if($_POST['ID_MMSpecimen'] != "") { 
  foreach ($_POST['ID_MMSpecimen'] as $index => $ID_MMSpecimen)
  {
  $MMSpecimenPath = addslashes($_POST['formMMPath'][$index]);
  $MMSpecimenType = addslashes($_POST['formMMType'][$index]);
  $MMSpecimenCreator = addslashes($_POST['formMMCreator'][$index]);
  $MMSpecimenCreated = addslashes($_POST['formMMCreated'][$index]);
  $MMSpecimenContext = addslashes($_POST['formMMContext'][$index]);
  $MMSpecimenComment = addslashes($_POST['formMMComment'][$index]);
  $MMSpecimenLicense = addslashes($_POST['formMMLicense'][$index]);
  
  $updateMMSpecimen = "UPDATE sptoolmultimedia SET File_Path = '$MMSpecimenPath', File_Type = '$MMSpecimenType', File_Created_When = '$MMSpecimenCreated', File_Created_Who = '$MMSpecimenCreator', File_Context = '$MMSpecimenContext', File_Comment = '$MMSpecimenComment', File_License = '$MMSpecimenLicense', Updated_Who = '$log', Updated_When = '$Current_Date' WHERE ID_Multimedia = '$ID_MMSpecimen'";
  $resultupdateMMSpecimen = mysqli_query($GLOBALS["___mysqli_ston"], $updateMMSpecimen);
 


if(!$resultupdateMMSpecimen)
{ $msg = $updateMMSpecimen."\n";
  $msg .= "####Error updateMMSpecimen####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  include ("../config/Errorreport.php");   }
  
#############################################################################################################       
     
     } 
   }  // foreach ($_POST['ID_MMSpecimen'] as $index => $ID_MMSpecimen)
   
   
 if($_POST['ID_MMSite'] != "") { 
   foreach ($_POST['ID_MMSite'] as $index => $ID_MMSite)
  {
  $MMSitePath = addslashes($_POST['formMMSitePath'][$index]);
  $MMSiteType = addslashes($_POST['formMMSiteType'][$index]);
  $MMSiteCreator = addslashes($_POST['formMMSiteCreator'][$index]);
  $MMSiteCreated = addslashes($_POST['formMMSiteCreated'][$index]);
  $MMSiteContext = addslashes($_POST['formMMSiteContext'][$index]);
  $MMSiteComment = addslashes($_POST['formMMSiteComment'][$index]);
  $MMSiteLicense = addslashes($_POST['formMMSiteLicense'][$index]);
  
  $updateMMSite = "UPDATE sptoolmultimedia SET File_Path = '$MMSitePath', File_Type = '$MMSiteType', File_Created_When = '$MMSiteCreated', File_Created_Who = '$MMSiteCreator', File_Context = '$MMSiteContext', File_Comment = '$MMSiteComment', File_License = '$MMSiteLicense', Updated_Who = '$log', Updated_When = '$Current_Date' WHERE ID_Multimedia = '$ID_MMSite'";
  $resultupdateMMSite = mysqli_query($GLOBALS["___mysqli_ston"], $updateMMSite);
 


if(!$resultupdateMMSite)
{ $msg = $updateMMSite."\n";
  $msg .= "####Error updateMMSite####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  include ("../config/Errorreport.php");   }
  
#############################################################################################################       
     
     } 
   }  // foreach ($_POST['ID_MMSite'] as $index => $ID_MMSite)
   
   
   include("addmultimedia.php");
    
    //********************End Multimedia Data****************************
    
 //********************Start Genetic Accessions**************************
    
 if($_POST['ID_GeneticAccession'] != "") { 
  foreach ($_POST['ID_GeneticAccession'] as $index => $ID_GeneticAccession)
  {
  $GeneticLocus = $_POST['formGeneticLocus'][$index];
  $GenBankNumber  = addslashes($_POST['formGenbankNumber'][$index]);
  $SampleID = addslashes($_POST['formSampleID'][$index]);
  $bold = $_POST['bold'][$index];
	
  $updateGenAcc = "UPDATE sptoolgeneticaccessions SET FK_GeneticLocus = '$GeneticLocus', GenBankNumber= '$GenBankNumber', SampleID = '$SampleID', BOLD = '$bold', Updated_Who = '$log', Updated_When = '$Current_Date' WHERE ID_GeneticAccession = '$ID_GeneticAccession'";
  $resultupdateGenAcc = mysqli_query($GLOBALS["___mysqli_ston"], $updateGenAcc);
 
if(!$resultupdateGenAcc)
{ $msg = $updateGenAcc."\n";
  $msg .= "####Error updateGenBank####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  include ("../config/Errorreport.php");   }
  
#############################################################################################################       
     
     } 
   }  // foreach ($_POST['ID_GenBank'] as $index => $ID_GenBank)


//start new genetic accession numbers
 if (!isset($moreGenBank))
{       unset($formGeneticLocusA);
        unset($formGenbankNumberA); 
		unset($formSampleIDA);
        unset($boldA);
        unset($formGeneticLocusB);
        unset($formGenbankNumberB);
		unset($formSampleIDB);
        unset($boldB);
        unset($formGeneticLocusC);
        unset($formGenbankNumberC); 
		unset($formSampleIDC);
        unset($boldC);
        unset($formGeneticLocusD);
        unset($formGenbankNumberD); 
		unset($formSampleIDD);
        unset($boldD);  
		unset($formGeneticLocusE);
        unset($formGenbankNumberE); 
		unset($formSampleIDE);
        unset($boldE);		
        }
else {
if($formGenBankNumberA)
 {
   $sql = "INSERT INTO sptoolgeneticaccessions (FK_Collection, FK_GeneticLocus, GenBankNumber, SampleID, BOLD, Created_Who)" .
                          "VALUES ('$IDsptoolCollection','$formGeneticLocusA','$sqlGenBankNumberA','$sqlSampleIDA','$boldA','$log')";

  $result = mysqli_query($GLOBALS["___mysqli_ston"], $sql);
                          
#######################################Error formGenbankNumberA###############################################################

if(!$result)
{ $msg = $sql."\n";
  $msg .= "####Error formGenbankNumberA####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  /**
  * calling Error message
  */
  include ("../config/Errorreport.php");   } 
  
  
#############################################################################################################                           
 }
 if($formGenBankNumberB)
 {
  $sql = "INSERT INTO sptoolgeneticaccessions (FK_Collection, FK_GeneticLocus, GenBankNumber, SampleID, BOLD, Created_Who)" .
                          "VALUES ('$IDsptoolCollection','$formGeneticLocusB','$sqlGenBankNumberB','$sqlSampleIDB','$boldB','$log')";
  $result = mysqli_query($GLOBALS["___mysqli_ston"], $sql);
                          
#######################################Error formGenbankNumberB###############################################################

if(!$result)
{ $msg = $sql."\n";
  $msg .= "####Error formGenbankNumberB####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  /**
  * calling Error message
  */  
  include ("../config/Errorreport.php");   }
  
#############################################################################################################         
 }  
 if($formGenBankNumberC)
 {
  $sql = "INSERT INTO sptoolgeneticaccessions (FK_Collection, FK_GeneticLocus, GenBankNumber, SampleID, BOLD, Created_Who)" .
                          "VALUES ('$IDsptoolCollection','$formGeneticLocusC','$sqlGenBankNumberC','$sqlSampleIDC','$boldC','$log')";
  $result = mysqli_query($GLOBALS["___mysqli_ston"], $sql);
                          
#######################################Error formGenbankNumberC###############################################################

if(!$result)
{ $msg = $sql."\n";
  $msg .= "####Error formGenbankNumberC####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  /**
  * calling Error message
  */  
  include ("../config/Errorreport.php");   }
  
#############################################################################################################                           
 }   
 if($formGenBankNumberD)
 {
  $sql = "INSERT INTO sptoolgeneticaccessions (FK_Collection, FK_GeneticLocus, GenBankNumber, SampleID, BOLD, Created_Who)" .
                          "VALUES ('$IDsptoolCollection','$formGeneticLocusD','$sqlGenBankNumberD','$sqlSampleIDD','$boldD','$log')";
  $result = mysqli_query($GLOBALS["___mysqli_ston"], $sql);
                          
#######################################Error formGenbankNumberD###############################################################

if(!$result)
{ $msg = $sql."\n";
  $msg .= "####Error formGenbankNumberD####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  /**
  * calling Error message
  */  
  include ("../config/Errorreport.php");   }
  
#############################################################################################################  
 }     
if($formGenBankNumberE)
 {
  $sql = "INSERT INTO sptoolgeneticaccessions (FK_Collection, FK_GeneticLocus, GenBankNumber, SampleID, BOLD, Created_Who)" .
                          "VALUES ('$IDsptoolCollection','$formGeneticLocusE','$sqlGenBankNumberE','$sqlSampleIDE','$boldE','$log')";
  $result = mysqli_query($GLOBALS["___mysqli_ston"], $sql);
                          
#######################################Error formGenbankNumberE###############################################################

if(!$result)
{ $msg = $sql."\n";
  $msg .= "####Error formGenbankNumberE####";
  if($debug == '1') echo $msg;
  trigger_error($msg, E_USER_ERROR);
  /**
  * calling Error message
  */  
  include ("../config/Errorreport.php");   }
  
#############################################################################################################  
 }  
} 
//end new genetic accession numbers   
   
    //********************End Genetic Accessions****************************	   
   include("addvoucherref.php");
   if($AddHost != "") {
   include("addhost.php"); }
   
  
    }
}
}
?>