PDA

View Full Version : Cascade delete


lux
02-20-2007, 08:58 AM
hey,

I have the situation where i want to delete a record but its id appears as a forgien key in another table and my db won't allow me to delete the record.

my code for deleting a record is:


$delete = $_GET['del'];
if($delete)
{
$query = sprintf("DELETE FROM tbl_dept WHERE deptid='$delete'");
$delete = pg_exec($query);
header('location:departments.php');

}


i pass the variable 'del' in the url when the user clicks on some text.

an the moment this returns an error because i have records in another table that contain the id of the dept which i am trying to delete.

can anyone advise on how to cascade delete the rows in the second table?

cheers

davidj
02-20-2007, 09:08 AM
add an aditional delete under the original


$query = sprintf("DELETE FROM tbl_dept WHERE deptid='$delete'");
$delete = pg_exec($query);


$query = sprintf("DELETE FROM tbl2 WHERE deptid='$delete'");
$delete = pg_exec($query);


header('location:departments.php');

davidj
02-20-2007, 09:10 AM
you might have to delete the ones from the table thats throwing the error first

lux
02-20-2007, 09:18 AM
yeah cool cheers for that!

ill add a pop up window to let the user know whats going on and ask whether want to proceed.