source: Admin Actions/DeleteAuthor.php@ 75f74d9

main
Last change on this file since 75f74d9 was 75f74d9, checked in by Vlado 222039 <vlado.popovski@…>, 6 weeks ago

Initial commit: Adding Book Tracker code

  • Property mode set to 100644
File size: 1.3 KB
Line 
1<?php
2require_once '../connect.php';
3
4if ($_SERVER['REQUEST_METHOD'] === 'POST') {
5 try {
6 $conn->beginTransaction();
7
8 $authorId = $_POST['authorId'];
9
10 $getImageQuery = "SELECT author_image FROM author WHERE authorid = :authorId";
11 $imageStmt = $conn->prepare($getImageQuery);
12 $imageStmt->execute([':authorId' => $authorId]);
13 $imageResult = $imageStmt->fetch(PDO::FETCH_ASSOC);
14
15 // Delete the author
16 $query = "DELETE FROM author WHERE authorid = :authorId";
17 $stmt = $conn->prepare($query);
18 $stmt->execute([':authorId' => $authorId]);
19
20 // If author had an image, delete the file
21 if ($imageResult && $imageResult['author_image']) {
22 $imagePath = 'AuthorImages/' . $imageResult['author_image'];
23 if (file_exists($imagePath)) {
24 unlink($imagePath);
25 }
26 }
27
28 $conn->commit();
29
30 echo json_encode(['success' => true]);
31
32 } catch(PDOException $e) {
33 if ($conn->inTransaction()) {
34 $conn->rollBack();
35 }
36 echo json_encode(['success' => false, 'error' => $e->getMessage()]);
37 } catch(Exception $e) {
38 if ($conn->inTransaction()) {
39 $conn->rollBack();
40 }
41 echo json_encode(['success' => false, 'error' => $e->getMessage()]);
42 }
43}
44?>
Note: See TracBrowser for help on using the repository browser.