Moved code to delete an Instances record and all its references in other tables to function delinstbyid in lib/delinstbyid.php; minor changes

This commit is contained in:
pezcurrel 2022-12-18 11:44:11 +01:00
parent 2b0e2398ae
commit ccc9f517fd

View file

@ -18,7 +18,9 @@
define('N',"\n");
define('SNAME',basename(__FILE__));
define('CONFIGFP',__DIR__.'/../../conf/mustard.ini');
define('CONFIGFP',__DIR__.'/../conf/mustard.ini');
require(__DIR__.'/lib/delinstbyid.php');
$help='SYNOPSYS
'.SNAME.' [options]
@ -57,7 +59,7 @@ use function mysqli_real_escape_string as myesc;
function mexit($msg,$code) {
global $link;
if (isset($link)) mysqli_close($link);
if (isset($link) && $link!==false) mysqli_close($link);
if ($code>0)
fwrite(STDERR,$msg);
else
@ -143,34 +145,9 @@ if ($tot>0 && $opts['delete']) {
echo(N.'Ok, ill keep the record with ID='.$recs[$inp]['id'].N);
foreach ($recs as $key=>$rec) {
if ($key!=$inp) {
echo('Deleting record with ID='.$rec['id'].' ...'.N);
myq($link,'DELETE FROM Instances WHERE ID='.$rec['id']);
echo('Deleted '.mysqli_affected_rows($link).' records from Instances table.'.N);
myq($link,'DELETE FROM InstActivity WHERE InstID='.$rec['id']);
echo('Deleted '.mysqli_affected_rows($link).' records from InstActivity table.'.N);
myq($link,'DELETE FROM InstChecks WHERE InstID='.$rec['id']);
echo('Deleted '.mysqli_affected_rows($link).' records from InstChecks table.'.N);
myq($link,'DELETE FROM InstFinancing WHERE InstID='.$rec['id']);
echo('Deleted '.mysqli_affected_rows($link).' records from InstFinancing table.'.N);
myq($link,'DELETE FROM InstLangs WHERE InstID='.$rec['id']);
echo('Deleted '.mysqli_affected_rows($link).' records from InstLangs table.'.N);
myq($link,'DELETE FROM InstOurLangs WHERE InstID='.$rec['id']);
echo('Deleted '.mysqli_affected_rows($link).' records from InstOurLangs table.'.N);
myq($link,'DELETE FROM InstPolicies WHERE InstID='.$rec['id']);
echo('Deleted '.mysqli_affected_rows($link).' records from InstPolicies table.'.N);
myq($link,'DELETE FROM InstTags WHERE InstID='.$rec['id']);
echo('Deleted '.mysqli_affected_rows($link).' records from InstTags table.'.N);
myq($link,'DELETE FROM InstTrends WHERE InstID='.$rec['id']);
echo('Deleted '.mysqli_affected_rows($link).' records from InstTrends table.'.N);
$users=myq($link,'SELECT ID FROM Users WHERE InstID='.$rec['id']);
$iii=0;
while ($user=mysqli_fetch_assoc($users)) {
myq($link,'DELETE FROM UsersFields WHERE UserID='.$user['ID']);
$iii+=mysqli_affected_rows($link);
}
echo('Deleted '.$iii.' records from UsersFields table.'.N);
myq($link,'DELETE FROM Users WHERE InstID='.$rec['id']);
echo('Deleted '.mysqli_affected_rows($link).' records from Users table.'.N);
$res=delinstbyid($link,$rec['id']);
foreach ($res['log'] as $logent) echo($logent.N);
if (!$res['ok']) mexit('ERROR: see the last message above this line.'.N,2);
}
}
echo(N);