News:

Wondering if this will always be free?  See why free is better.

Main Menu

Deleting from multipy tables

Started by mickjav, December 27, 2023, 10:21:29 AM

Previous topic - Next topic

mickjav

The Function below is designed to remove a person if there are no records in:
people_family_members
people_married

and only 1 entry in people_groups

What I think is happening is when no records are found in one of the delete statements no other delete statements run but the redirectexit runs

If I move the one below to first place, it gets deleted?
//Now do the person record
$smcFunc['db_query']('', "DELETE FROM {db_prefix}people WHERE person_id = $person LIMIT 1");

Haven't a clue what I've done wrong

any help would be appreciated

public function DoDeletePerson()
{

global $smcFunc, $txt;

isAllowedTo('music_can_delete_artists');
$can_delete = true;
$row = array();

/*
Items to be deleted in order of deletion
people_and_galleries DONE
people_biographies DONE
people_influences DONE
people_movements DONE
people_quotes DONE
people_groups DONE
Person DONE

if items found person cant be deleted until they are removed
this is also done to prevent error deletes
people_family_members
people_married (if one there will be a details record plus other side of marrage)
people_groups can only be one
*/

$person = (int) $_REQUEST['pers'];

if (empty($person))
fatal_error($txt['people_no_person_found'], false);

//Check for people_family_members
$familyquery = $smcFunc['db_query']('', '
SELECT id
FROM {db_prefix}people_family_members
WHERE person_id = {int:this_person}',
[
'this_person' => $person,
]
);
if ($smcFunc['db_affected_rows']() != 0)
$can_delete = false;

$smcFunc['db_free_result']($familyquery);

//Check for people_married
$marriedquery = $smcFunc['db_query']('', '
SELECT id_married
FROM {db_prefix}people_married
WHERE person_id = {int:this_person}',
[
'this_person' => $person,
]
);
if ($smcFunc['db_affected_rows']() != 0)
$can_delete = false;

$smcFunc['db_free_result']($marriedquery);

//Check no more than 1 group records
$movequery = $smcFunc['db_query']('', '
SELECT move_id
FROM {db_prefix}people_groups
WHERE person_id = {int:this_person}',
[
'this_person' => $person,
]
);

     // grab the move id to be able to delete it
if ($smcFunc['db_num_rows']($movequery) > 1 )
$can_delete = false;
elseif ($smcFunc['db_num_rows']($movequery) == 1 )
$row = $smcFunc['db_fetch_assoc']($movequery);

if ($can_delete == false)
fatal_error($txt['people_cannot_delete_person'], false);



     //if we got here then all tests pasted
//people_and_galleries will be more than one record
if ($smcFunc['db_free_result']($movequery) == 1)
$smcFunc['db_query']('', "DELETE FROM {db_prefix}people_groups WHERE person_id = $person");

//people_movements may be more than one record
if ($row)
$smcFunc['db_query']('', "DELETE FROM {db_prefix}people_movements WHERE move_id =" . $row['move_id']);

//people_and_galleries will be more than one record
$smcFunc['db_query']('', "DELETE FROM {db_prefix}people_and_galleries WHERE person_id = $person");
//people_biographies will be more than one record
$smcFunc['db_query']('', "DELETE FROM {db_prefix}people_biographies WHERE person_id = $person");
//people_influences will be more than one record
$smcFunc['db_query']('', "DELETE FROM {db_prefix}people_influences WHERE person_id = $person");
//quotes will be more than one record
$smcFunc['db_query']('', "DELETE FROM {db_prefix}people_quotes WHERE person_id = $person");
//Now do the person record
$smcFunc['db_query']('', "DELETE FROM {db_prefix}people WHERE person_id = $person LIMIT 1");

$smcFunc['db_free_result']($movequery);

// Redirect to the category listing
redirectexit('action=music;area=people;sa=index');

}



Advertisement: