-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.php
99 lines (73 loc) · 2.47 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php include '/my-php-code/to_do_list/config/database.php' ?>
<?php
$id = $_GET['id'] ?? null;
if (!$id) {
header('Location: index.php');
exit;
}
//fetch from database
$statement = $pdo->prepare('SELECT * FROM tasks WHERE id = :id');
$statement->bindValue(':id', $id);
$statement->execute();
$todos = $statement->fetch(PDO::FETCH_ASSOC);
// echo '<pre>';
// var_dump($todos);
// '</pre>';
// exit;
$task = $todos['Title'];
$itemId = $todos['id'];
$todoErr = '';
if (isset($_POST['submit'])) {
$task = filter_input(INPUT_POST, 'task', FILTER_SANITIZE_SPECIAL_CHARS);
$id =filter_input(INPUT_POST, 'id', FILTER_SANITIZE_SPECIAL_CHARS);
if (!$task) {
$todoErr = 'Todo is required!!';
}
//Adding to database
if (empty($todoErr)) {
$sql = "UPDATE tasks SET Title = :title WHERE id = :id";
var_dump($sql);
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':title', $task);
$stmt->bindParam(':id', $itemId, PDO::PARAM_INT);
$stmt->execute();
// Redirect the user back to the todo list page or show a success message
header("Location: index.php");
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="./bootstrap-offline/css/bootstrap.css">
<script src="./bootstrap-offline/js/jquery-3.6.0.js"></script>
<script src="./bootstrap-offline/js/bootstrap.js"></script>
<title>To do list</title>
</head>
<body>
<p>
<a href="index.php" class="btn btn-secondary">Go back to List</a>
</p>
<div class="container d-flex flex-column align-items-center mt-5">
<h1><?php echo "Whoop it's " . date('l') . '!'; ?></h1>
<form class="mt-4 d-flex flex-row w-75 justify-content-center" method="POST">
<!-- Name input -->
<div class="mb-3 w-100">
<input value="<?php echo $task ?>" type="text" maxlength="50" class="form-control px-5 w-100 <?php echo $todoErr ? 'is-invalid' : null; ?>" id="task" name="task" placeholder="What do you want to do today ?...">
<div class="invalid-feedback">
<?php echo $todoErr ?>
</div>
</div>
<input type="hidden" value="<?php echo $id ?>" name="id" class="form-control">
<div class="mb-3 form-group">
<input type="submit" name="submit" value="Update" class="btn btn-dark w-100">
</div>
</form>
</div>
</body>
</html>