Quantcast
Channel: Editor — DataTables forums
Viewing all articles
Browse latest Browse all 3740

Delete a row from a related table on $_POST['action'] === 'remove'

$
0
0

What I would like to do is delete an orphan record in a joined table if it exists. The $_POST['action'] === 'remove' only passes back the id (PK) of the main table to the server, which is not referenced in my joined table. Can I return an additional value ? Eg my contacts table contains contactid and memberid. On deleting the contact, I may want to delete the memberid in the members table.

I have tried using the contactid to lookup the memberid and then perform a delete, but it doesn’t quite work (seems to take 2 attempts to delete) and seems a bit convoluted. Is there a better way, or have I got my code running in the wrong order ?

if ( isset($_POST['action']) && $_POST['action'] === 'remove' ) {
//retrieve value(s) from editor and format as a comma separated string
//these are the selected contactids being removed from the contacts table
//we need to pass these values back into a lookup query to determine which memberids they correspond to
//we then use these memberids to delete rows from tblmembers
$rowid = ltrim(str_replace('row_',',',implode($_POST['id'])),",");
//print_r($rowid);

$lookup = $db->sql( "SELECT MemberID FROM tblcontacts WHERE ContactID IN (".$rowid.")");
//print_r($lookup);
while($row = $lookup->fetch()) {
    //echo $row['MemberID'];
    $sql = $db->sql( "DELETE FROM tblmembers WHERE MemberID = ".$row['MemberID']."");
}

Viewing all articles
Browse latest Browse all 3740

Trending Articles