-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.php
89 lines (65 loc) · 2.59 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
<?php
session_start();
$userId = $_SESSION["userId"];
require "db_connection.php";
// require "functions.php";
if (isset($_GET["dosageId"])) {
try {
$plan_id = $_GET["dosageId"];
$data = getDosagePlanById($connection, $plan_id);
echo json_encode($data);
} catch (Exception $ex) {
echo json_encode(array("errors" => $ex->getMessage()));
}
return;
}
if (isset($_POST["isDosagePlanUpdate"])) {
if ($_POST["plan_id"] != null && $_POST["isDosagePlanUpdate"] == true) {
try {
$plan_id = $_POST["plan_id"];
$medicine_id = trim($_POST["medicine_id"]);
$date_taken = trim($_POST["date_taken"]);
$time_taken = trim($_POST["time_taken"]);
if (getDosagePlanById($connection, $plan_id) != null) {
$sql = "UPDATE tbldosageplanner SET medicine_id = :medicine_id, date_taken = :date_taken, time_taken = :time_taken,user_id = :user_id where plan_id = :plan_id";
if ($stmt = $connection->prepare($sql)) {
$stmt->bindParam(":medicine_id", $medicine_id, PDO::PARAM_INT);
$stmt->bindParam(":date_taken", $date_taken, PDO::PARAM_STR);
$stmt->bindParam(":time_taken", $time_taken, PDO::PARAM_STR);
$stmt->bindParam(":plan_id", $plan_id, PDO::PARAM_INT);
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
if ($stmt->execute()) {
echo json_encode(array("success" => "Dosage Plan Successfully Updated"));
} else {
echo json_encode(array("error" => "Error Updating Dosage Plan"));
}
} else {
echo json_encode(array("error" => "Oops! Something went wrong"));
}
} else {
echo json_encode(array("error" => "Dosage Plan Does not Exist!"));
}
} catch (Exception $ex) {
echo json_encode(array("errors" => $ex->getMessage()));
}
}
} else {
echo json_encode("No Plan ID or User ID");
}
function getDosagePlanById($connection, $dosageId)
{
try {
$sql = "SELECT * FROM tbl_dosages WHERE dosageId = :dosageId";
$stmt = $connection->prepare($sql);
$stmt->bindParam(":dosageId", $dosageId, PDO::PARAM_INT);
if ($stmt->execute()) {
return $stmt->fetch(PDO::FETCH_ASSOC);
} else {
return null;
}
} catch (Exception $ex) {
throw $ex;
} finally {
$connection = null;
}
}