-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.php
80 lines (66 loc) · 2.17 KB
/
update.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/mongo_atlas_setup.php';
use MongoDB\BSON\ObjectId;
$movies_list = getMongoCollection('movie_list', 'movies');
$title = '';
$year = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$filter = ['_id' => new ObjectId($_POST['id'])];
$update = ['$set' => [
'movie_title' => $_POST['movie-title'],
'movie_year' => (int)$_POST['movie-year'],
]];
$result = $movies_list->updateOne($filter, $update);
if ($result->getModifiedCount() === 1) {
// echo "Movie updated successfully!";
header('Location: ' . '/');
} else {
echo "Failed to update movie.";
}
} else {
if (isset($_GET['id'])) {
$id = $_GET['id'];
$movie = $movies_list->findOne(['_id' => new ObjectId($id)]);
if ($movie) {
$title = $movie['movie_title'];
$year = $movie['movie_year'];
} else {
echo "Movie not found.";
exit;
}
} else {
echo "No movie ID provided.";
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
<title>Update Movie </title>
</head>
<body class="bg-gray-100 p-10">
<div class="max-w-4xl mx-auto bg-white p-6 rounded shadow">
<h1 class="text-3xl font-bold mb-4">Update <?= $title ?> </h1>
<form method="POST" action="update.php" class="space-y-4">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div>
<label class="block text-gray-700">Title</label>
<input type="text" name="movie-title" value="<?= htmlspecialchars($title); ?>" required class="w-full p-2 border rounded" />
</div>
<div>
<label class="block text-gray-700">Release Year</label>
<input type="number" name="movie-year" value="<?= htmlspecialchars($year); ?>" required class="w-full p-2 border rounded" />
</div>
<button class=" bg-green-500 text-white py-2 px-4 rounded" type="submit">Update</button>
</form>
<div class="mt-4">
<a href="index.php" class="bg-gray-500 text-white py-2 px-4 rounded">Back to List</a>
</div>
</div>
</body>
</html>