PHP Transaction if query success do ..
Ik ben nieuw in PHP transactions. Ik heb het altijd links laten liggen maar wil het nu toch echt gebruiken aangezien ik een actie heb gemaakt waarbij een tijdlijn bericht kan worden verwijderd en daarmee dus ook alle reacties en likes verwijderd moeten worden.
Dit werkt goed alleen wil ik graag dat de reacties en likes verwijder query pas wordt uitgevoerd als de eerste query succesvol is uitgevoerd. In de eerste query check ik namelijk of de userid wel overeenkomt met de gene die de actie uitvoert. Het moet niet zo zijn dat als iemand een bericht van een ander niet kan verwijderen hij of zij wel de reacties en likes ervan weggooit.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
/* disable autocommit */
$conn->autocommit(FALSE);
// Delete Timeline Message
$conn->query("DELETE FROM timeline
WHERE id = '" . $TimelineID . "'
AND userid = '" . $Func_User_ID . "'");
// Delete Comments from Timeline Message
$conn->query("DELETE FROM comments
WHERE timeline_post_id = '" . $TimelineID . "'");
// Delete Likes from Timeline Message
$conn->query("DELETE FROM likes
WHERE timelineid = '" . $TimelineID . "'");
// Delete Notifications from Timeline Message
$conn->query("DELETE FROM notifications
WHERE correspondingid = '" . $TimelineID . "'
AND
(type = '1' OR type = '2')");
/* commit insert */
if ($conn->commit()) {
echo "Success";
}
$conn->close();
?>
/* disable autocommit */
$conn->autocommit(FALSE);
// Delete Timeline Message
$conn->query("DELETE FROM timeline
WHERE id = '" . $TimelineID . "'
AND userid = '" . $Func_User_ID . "'");
// Delete Comments from Timeline Message
$conn->query("DELETE FROM comments
WHERE timeline_post_id = '" . $TimelineID . "'");
// Delete Likes from Timeline Message
$conn->query("DELETE FROM likes
WHERE timelineid = '" . $TimelineID . "'");
// Delete Notifications from Timeline Message
$conn->query("DELETE FROM notifications
WHERE correspondingid = '" . $TimelineID . "'
AND
(type = '1' OR type = '2')");
/* commit insert */
if ($conn->commit()) {
echo "Success";
}
$conn->close();
?>
Gewijzigd op 10/01/2018 12:11:26 door Danny von Gaal
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
/* disable autocommit */
$conn->autocommit(FALSE);
// Delete Timeline Message
$conn->query("DELETE FROM timeline
WHERE id = '" . $TimelineID . "'
AND userid = '" . $Func_User_ID . "'");
if ($conn->affected_rows == 1) {
// Delete Comments from Timeline Message
$conn->query("DELETE FROM comments
WHERE timeline_post_id = '" . $TimelineID . "'");
// Delete Likes from Timeline Message
$conn->query("DELETE FROM likes
WHERE timelineid = '" . $TimelineID . "'");
// Delete Notifications from Timeline Message
$conn->query("DELETE FROM notifications
WHERE correspondingid = '" . $TimelineID . "'
AND
(type = '1' OR type = '2')");
/* commit insert */
if ($conn->commit()) {
echo "Success";
}
} else {
$conn->rollback();
}
$conn->close();
?>
/* disable autocommit */
$conn->autocommit(FALSE);
// Delete Timeline Message
$conn->query("DELETE FROM timeline
WHERE id = '" . $TimelineID . "'
AND userid = '" . $Func_User_ID . "'");
if ($conn->affected_rows == 1) {
// Delete Comments from Timeline Message
$conn->query("DELETE FROM comments
WHERE timeline_post_id = '" . $TimelineID . "'");
// Delete Likes from Timeline Message
$conn->query("DELETE FROM likes
WHERE timelineid = '" . $TimelineID . "'");
// Delete Notifications from Timeline Message
$conn->query("DELETE FROM notifications
WHERE correspondingid = '" . $TimelineID . "'
AND
(type = '1' OR type = '2')");
/* commit insert */
if ($conn->commit()) {
echo "Success";
}
} else {
$conn->rollback();
}
$conn->close();
?>
Je kan ook de tabellen onder de tabel op het hoogste niveau aanmaken met cascading delete dan hoef je het niet uit te programmeren. Werkt veel beter dan al die losse deletes.
Waarom check je dit dan niet als eerste en doe je de rest daarna?
Wat @John D zei, en ik zie hier niet echt de meerwaarde van een transactie. Je vergrendelt namelijk niets voordat je alles gewoon wegkiepert.
Ozzie PHP op 10/01/2018 14:37:21:
>> Dit werkt goed alleen wil ik graag dat de reacties en likes verwijder query pas wordt uitgevoerd als de eerste query succesvol is uitgevoerd.
Waarom check je dit dan niet als eerste en doe je de rest daarna?
Waarom check je dit dan niet als eerste en doe je de rest daarna?
Omdat ik niet wil dat er comments en likes blijven hangen als het hoofd bericht weg is. Mocht er ergens een error optreden dan heb ik liever dat er helemaal niets weg is om vervuiling te voorkomen.
Ik heb gezocht op cascading delete maar vind niet ergens een goed voorbeeld. Dit bericht erover zegt wel precies wat ik wil maar heeft iemand een voorbeeld?
Quote:
If you like the Parent and Child terms and you feel they are easy to be remembered, you may like the translation of ON DELETE CASCADE to Leave No Orphans!
Which means that when a Parent row is deleted (killed), no orphan row should stay alive in the Child table. All children of the parent row are killed (deleted), too. If any of these children has grandchildren (in another table through another foreign key) and there is ON DELETE CASCADE defined, these should be killed, too (and all descendants, as long as there is a cascade effect defined.)
The FOREIGN KEY constraint itself could also be described as Allow No Orphans! (in the first place). No Child should ever be allowed (written) in the child table if it hasn't a Parent (a row in the parent table).
For consistency, the ON DELETE RESTRICT can be translated to the (less aggresive) You Can't Kill Parents! Only childless rows can be killed (deleted.)
Which means that when a Parent row is deleted (killed), no orphan row should stay alive in the Child table. All children of the parent row are killed (deleted), too. If any of these children has grandchildren (in another table through another foreign key) and there is ON DELETE CASCADE defined, these should be killed, too (and all descendants, as long as there is a cascade effect defined.)
The FOREIGN KEY constraint itself could also be described as Allow No Orphans! (in the first place). No Child should ever be allowed (written) in the child table if it hasn't a Parent (a row in the parent table).
For consistency, the ON DELETE RESTRICT can be translated to the (less aggresive) You Can't Kill Parents! Only childless rows can be killed (deleted.)
Danny von Gaal op 10/01/2018 16:43:07:
Ik heb gezocht op cascading delete maar vind niet ergens een goed voorbeeld. Dit bericht erover zegt wel precies wat ik wil maar heeft iemand een voorbeeld?
Helemaal uitgelegd hier: http://www.mysqltutorial.org/mysql-on-delete-cascade/