-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoggle-to-do-status.php
63 lines (52 loc) · 2.03 KB
/
toggle-to-do-status.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
<?php
// Include your database configuration file
include "db_config.php";
// Create connection
$conn = new mysqli($servername, $username, $dbpassword, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// User input (example)
$item_id = intval($_POST['item_id']);
$user_id = intval($_POST['user_id']);
$description = $_POST['description'];
$status = $_POST['current_status'];
$sql = "";
if ($status == '0'){
// unchecked -> check
$sql = "DELETE FROM `todolist` WHERE user_id = $user_id AND item_id = $item_id";
$result1 = $conn->query($sql);
if ($result1) {
$sql = "INSERT INTO todolist (item_id, user_id, description, checked) VALUES ($item_id, $user_id, '$description', 1)";
$result2 = $conn->query($sql);
if ($result2) {
$response = array("success" => true, "message" => "Query successful");
} else {
$response = array("success" => false, "message" => "Insert failed: " . mysqli_error($conn));
}
} else {
$response = array("success" => false, "message" => "Delete failed: " . mysqli_error($conn));
}
} elseif ($status == '1'){
// checked -> unchecked
$sql = "DELETE FROM `todolist` WHERE user_id = $user_id AND item_id = $item_id";
$result1 = $conn->query($sql);
if ($result1) {
$sql = "INSERT INTO todolist (item_id, user_id, description, checked) VALUES ($item_id, $user_id, '$description', 0)";
$result2 = $conn->query($sql);
if ($result2) {
$response = array("success" => true, "message" => "Query successful");
} else {
$response = array("success" => false, "message" => "Insert failed: " . mysqli_error($conn));
}
} else {
$response = array("success" => false, "message" => "Delete failed: " . mysqli_error($conn));
}
} else {
$response = array("success" => false, "message" => "status was not type character");
}
echo json_encode($response);
// Close the database connection
$conn->close();
?>