1 | <?php
|
---|
2 | require_once '../connect.php';
|
---|
3 |
|
---|
4 | header('Content-Type: application/json');
|
---|
5 | error_reporting(0);
|
---|
6 |
|
---|
7 | if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
---|
8 | try {
|
---|
9 |
|
---|
10 | $required = ['bookId', 'isbn', 'title', 'genre', 'publishedYear', 'description', 'totalCopies'];
|
---|
11 | foreach ($required as $field) {
|
---|
12 | if (!isset($_POST[$field])) {
|
---|
13 | throw new Exception("Missing required field: $field");
|
---|
14 | }
|
---|
15 | }
|
---|
16 |
|
---|
17 | $bookId = $_POST['bookId'];
|
---|
18 | if (!is_numeric($bookId)) {
|
---|
19 | throw new Exception('Invalid book ID');
|
---|
20 | }
|
---|
21 |
|
---|
22 | // Initialize variables
|
---|
23 | $imageName = null;
|
---|
24 | $oldImage = null;
|
---|
25 | $uploadDir = __DIR__ . '/../BookImages/';
|
---|
26 |
|
---|
27 | $stmt = $conn->prepare("SELECT coverimage FROM book WHERE bookid = ?");
|
---|
28 | $stmt->execute([$bookId]);
|
---|
29 | $oldImage = $stmt->fetchColumn();
|
---|
30 |
|
---|
31 | // Handle file upload
|
---|
32 | if (isset($_FILES['coverImage']) && $_FILES['coverImage']['error'] === UPLOAD_ERR_OK) {
|
---|
33 | // Create directory if not exists
|
---|
34 | if (!file_exists($uploadDir)) {
|
---|
35 | mkdir($uploadDir, 0777, true);
|
---|
36 | }
|
---|
37 |
|
---|
38 | $fileExtension = strtolower(pathinfo($_FILES['coverImage']['name'], PATHINFO_EXTENSION));
|
---|
39 | $allowedTypes = ['jpg', 'jpeg', 'png', 'gif'];
|
---|
40 | if (!in_array($fileExtension, $allowedTypes)) {
|
---|
41 | throw new Exception('Invalid file type. Allowed: JPG, JPEG, PNG, GIF.');
|
---|
42 | }
|
---|
43 |
|
---|
44 | // Generate unique filename
|
---|
45 | $fileName = uniqid() . '_' . basename($_FILES['coverImage']['name']);
|
---|
46 | $targetPath = $uploadDir . $fileName;
|
---|
47 |
|
---|
48 | if (!move_uploaded_file($_FILES['coverImage']['tmp_name'], $targetPath)) {
|
---|
49 | throw new Exception('Failed to upload image.');
|
---|
50 | }
|
---|
51 | $imageName = $fileName;
|
---|
52 | }
|
---|
53 |
|
---|
54 | // Start transaction
|
---|
55 | $conn->beginTransaction();
|
---|
56 |
|
---|
57 | $bookParams = [
|
---|
58 | ':isbn' => $_POST['isbn'],
|
---|
59 | ':title' => $_POST['title'],
|
---|
60 | ':genre' => $_POST['genre'],
|
---|
61 | ':publishedYear' => $_POST['publishedYear'],
|
---|
62 | ':description' => $_POST['description'],
|
---|
63 | ':totalCopies' => $_POST['totalCopies'],
|
---|
64 | ':bookId' => $bookId,
|
---|
65 | ':format' => $_POST['format'],
|
---|
66 | ':language' => $_POST['language'],
|
---|
67 | ':pages' => $_POST['pages'],
|
---|
68 | ':publisher' => $_POST['publisher'],
|
---|
69 | ];
|
---|
70 |
|
---|
71 | if ($imageName) {
|
---|
72 | $bookParams[':coverImage'] = $imageName;
|
---|
73 | }
|
---|
74 | else {
|
---|
75 | $bookParams[':coverImage'] = $oldImage;
|
---|
76 | }
|
---|
77 |
|
---|
78 | $stmt = $conn->prepare('CALL update_book(:bookId, :title, :isbn, :genre, :publishedYear, :description, :totalCopies, :coverImage, :language, :publisher, :pages, :format)');
|
---|
79 | $stmt->execute($bookParams);
|
---|
80 |
|
---|
81 | // Delete old image after successful update
|
---|
82 | if ($imageName && $oldImage && $imageName != $oldImage) {
|
---|
83 | $oldImagePath = $uploadDir . $oldImage;
|
---|
84 | if (file_exists($oldImagePath)) {
|
---|
85 | unlink($oldImagePath);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | $conn->commit();
|
---|
90 | echo json_encode(['success' => true, 'message' => 'Book updated successfully']);
|
---|
91 |
|
---|
92 | } catch (Exception $e) {
|
---|
93 | $conn->rollBack();
|
---|
94 | // Delete new image if error occurred
|
---|
95 | if (isset($targetPath) && file_exists($targetPath)) {
|
---|
96 | unlink($targetPath);
|
---|
97 | }
|
---|
98 | http_response_code(400);
|
---|
99 | echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | ?> |
---|