<?php

function delinstbyid(&$link,&$id,$efunc,$eol) {
	if (!query($link,'DELETE FROM Instances WHERE ID='.$id,'eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from Instances table.'.$eol);
	if (!query($link,'DELETE FROM InstActivity WHERE InstID='.$id,'eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from InstActivity table.'.$eol);
	if (!query($link,'DELETE FROM InstBlocks WHERE InstID='.$id,'eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from InstBlocks table.'.$eol);
	if (!query($link,'DELETE FROM InstChecks WHERE InstID='.$id,'eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from InstChecks table.'.$eol);
	if (!query($link,'DELETE FROM InstFinancing WHERE InstID='.$id,'eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from InstFinancing table.'.$eol);
	if (!query($link,'DELETE FROM InstLangs WHERE InstID='.$id,'eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from InstLangs table.'.$eol);
	if (!query($link,'DELETE FROM InstOurLangs WHERE InstID='.$id,'eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from InstOurLangs table.'.$eol);
	if (!query($link,'DELETE FROM InstPolicies WHERE InstID='.$id,'eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from InstPolicies table.'.$eol);
	if (!query($link,'DELETE FROM InstRules WHERE InstID='.$id,'eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from Instances table.'.$eol);
	if (!query($link,'DELETE FROM InstTags WHERE InstID='.$id,'eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from InstTags table.'.$eol);
	if (!query($link,'DELETE FROM InstTrends WHERE InstID='.$id,'eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from InstTrends table.'.$eol);
	if (!query($link,'DELETE FROM UsersFields WHERE UserID IN (SELECT ID FROM Users WHERE InstID='.$id.')','eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from UsersFields table.'.$eol);
	if (!query($link,'DELETE FROM Users WHERE InstID='.$id,'eecho',$eol)) return(false);
	$efunc('Deleted '.mysqli_affected_rows($link).' records from Users table.'.$eol);
	return(true);
}

function query(&$link,$query,$efunc,&$eol) {
	try {
		$res=mysqli_query($link,$query);
	}
	catch (Exception $error) {
		$efunc('Query «'.$query.'» failed: '.$error->getMessage().' ('.$error->getCode().').'.$eol);
		return(false);
	}
	// for php versions < 8, which seem to not catch mysql exceptions
	if ($res===false) {
		$efunc('Query «'.$query.'» failed: '.mysqli_errno($link).': '.mysqli_error($link).'.'.$eol);
		return(false);
	}
	return($res);
}

?>
