The project world is as follows:
I have a client, where I register for it, messages, news, upload photos, where there are calendars as well. So when I'm about to delete it, I need to delete everything from it, because the database does not allow for referential integrity. So far so good, but in the news section, I have another table that links the news images to that news, and this link is made by a table where I have the code of the news and the code of the image, in fact it is a link many For many. So, there is the problem, for me to delete the field of the table images related to that news is necessary that I delete before this relationship between the image and the news, but if I delete, I can not know what publication is that image .
How can I resolve this?
Follow what I've been doing so far.
mysql_query("DELETE FROM tbl_CLIENTES_AGENDA WHERE COD_IDENT_CLIEN ='" . $COD_IDENT_CLIEN . "'");
mysql_query("DELETE FROM tbl_IMAGENS
WHERE COD_SEQUN_IMAGM IN
(
SELECT COD_SEQUN_IMAGM FROM tbl_PUBLICACOESxIMAGENS
WHERE COD_IDENT_PUBLI IN
(
SELECT COD_IDENT_CLIEN FROM tbl_PUBLICACOES
WHERE COD_IDENT_CLIEN = " . $COD_IDENT_CLIEN . "
)
)");
mysql_query("DELETE FROM tbl_PUBLICACOES WHERE COD_IDENT_CLIEN = '" . $COD_IDENT_CLIEN . "'");
mysql_query("DELETE FROM tbl_RECADOS WHERE COD_IDENT_CLIEN = '" . $COD_IDENT_CLIEN . "");
mysql_query("DELETE FROM tbl_CLIENTES_PF WHERE COD_IDENT_CLIEN ='" . $COD_IDENT_CLIEN . "'");
mysql_query("DELETE FROM tbl_CLIENTES WHERE COD_IDENT_CLIEN ='" . $COD_IDENT_CLIEN . "'");
$query = mysql_query("SELECT * FROM tbl_IMAGENS I INNER JOIN tbl_CLIENTESxIMAGENS CI ON I.COD_SEQUN_IMAGM = CI.COD_SEQUN_IMAGM WHERE COD_IDENT_CLIEN ='" . $COD_IDENT_CLIEN . "' ");
while ($linha2 = mysql_fetch_array($query)) {
$arquivo = "../uploads/{$linha2['TXT_FILEN_IMAGN']}";
unlink($arquivo);
}
mysql_query("DELETE FROM tbl_CLIENTESxIMAGENS WHERE COD_IDENT_CLIEN ='" . $COD_IDENT_CLIEN . '"');
mysql_query("DELETE FROM tbl_IMAGENS WHERE COD_SEQUN_IMAGM IN
(
SELECT COD_SEQUN_IMAGM FROM tbl_CLIENTESxIMAGENS WHERE COD_IDENT_CLIEN = '" . $COD_IDENT_CLIEN . "'
)");
$sql = mysql_query("SELECT C.COD_IDENT_CLIEN, P.COD_IDENT_PUBLI, I.COD_SEQUN_IMAGM, I.TXT_FILEN_IMAGN
FROM
(
SELECT * FROM tbl_CLIENTES WHERE COD_IDENT_CLIEN = '" . $COD_IDENT_CLIEN . "'
) C
LEFT JOIN
(
SELECT * FROM tbl_PUBLICACOES
) P
ON P.COD_IDENT_CLIEN = C.COD_IDENT_CLIEN
LEFT JOIN
(
SELECT * FROM tbl_PUBLICACOESxIMAGENS
) PI
ON PI.COD_IDENT_PUBLI = P.COD_IDENT_PUBLI
LEFT JOIN
(
SELECT * FROM tbl_IMAGENS
) I
ON I.COD_SEQUN_IMAGM = PI.COD_SEQUN_IMAGM WHERE I.COD_SEQUN_IMAGM is not NULL;
");
while ($linha = mysql_fetch_array($sql)) {
$arquivo = "../uploads/{$linha['TXT_FILEN_IMAGN']}";
unlink($arquivo);
}
You need to follow a deletion order and it is: Deleting Schedule, Deleting ImagexPublishing, Deleting Image, Deleting Publication, Deleting Comments, Deleting client_Pf and last Deleting client.
An error is occurring when I delete the Publications / Images table, it reports the following error:
11:18:57 DELETE FROM tbl_PUBLICACOESxIMAGENS WHERE COD_SEQUN_IMAGM IN ('64, 65,66,67,68,70,71 ') 1 row (s) affected, 1 warning (s): 1292 Truncated incorrect DOUBLE value: '64, 65.66,67,68,70,71 '.374 sec.
The query I made is as follows:
DELETE FROM tbl_PUBLICACOESxIMAGENS WHERE COD_SEQUN_IMAGM IN ('64,65,66,67,68,70,71')