-
Notifications
You must be signed in to change notification settings - Fork 0
/
dosageplanner.php
50 lines (39 loc) · 1.53 KB
/
dosageplanner.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
<?php
session_start();
require "db_connection.php";
$user_id = $_SESSION["user_id"];
$errors = array();
if (isset($_POST["medicine_id"]) && isset($_POST["date_taken"]) && isset($_POST["time_taken"])) {
$medicine_id = trim($_POST["medicine_id"]);
$date_taken = trim($_POST["date_taken"]);
$time_taken = trim($_POST["time_taken"]);
if (empty($medicine_id)) {
$errors[] = ("Please select a medicine");
}
if (empty($date_taken)) {
$errors[] = ("Please select a date to take this medicine");
}
if (empty($time_taken)) {
$errors[] = ("Please select the time to take this medicine");
}
if (!empty($errors)) {
echo json_encode($errors);
} else {
try {
//save dosage plan to the database
$sql = "INSERT INTO tbldosageplanner (medicine_id,date_taken,time_taken,user_id) values (:medicine_id,:date_taken,:time_taken,:user_id);";
$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(":user_id", $user_id, PDO::PARAM_INT);
if ($stmt->execute()) {
echo json_encode(array("success" => "Dosage Planner Saved Successfully"));
}
} catch (Exception $ex) {
echo json_encode($ex->getMessage());
}
}
} else {
echo json_encode("Error Saving Plan");
}