Repository navigation
Expand file tree
/
Copy pathcreateConsult.php
More file actions
127 lines (103 loc) · 4.56 KB
/
Copy pathcreateConsult.php
File metadata and controls
127 lines (103 loc) · 4.56 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
include_once "conn.php";
if((!isset($_POST["weight"]) || empty($_POST["weight"]) || (!isset($_POST["veterinaryVAT"]) || empty($_POST["veterinaryVAT"])))){
echo('ERROR - Required fields not filled in.');
echo('<br>');
echo "<a href='index.php'> <button> Go back to homepage </button></a><br>";
}else{
// Start session to get variables from homepage
session_start();
$animalName = $_GET["animalName"];
$ownerName = $_GET["ownerName"];
$clientVAT = $_SESSION["clientVAT"];
$vetVAT = $_POST["veterinaryVAT"];
// Get current date
date_default_timezone_set('Europe/Lisbon');
$date = date("Y-m-d H:i:s");
//Get VAT from owner's name
try{
$sql = "select VAT from person where name=?;";
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $ownerName, PDO::PARAM_STR);
$stmt->execute();
foreach ($stmt as $row){
$ownerVAT = $row["VAT"];
}
}catch(PDOException $e){
echo("ERROR Couldnt access person." . $e->getMessage());
echo('<br>');
}
// INSERT CONSULT FIRST
if(!isset($_POST["S"]) || empty($_POST["S"])){
$s = 'default';
}else{
$s = $_POST["S"];
}
if(!isset($_POST["O"]) || empty($_POST["O"])){
$o = 'default';
}else{
$o = $_POST["O"];
}
if(!isset($_POST["A"]) || empty($_POST["A"])){
$a = 'default';
}else{
$a = $_POST["A"];
}
if(!isset($_POST["P"]) || empty($_POST["P"])){
$p = 'default';
}else{
$p = $_POST["P"];
}
try{
$sql = "INSERT INTO consult (name,VAT_owner, date_timestamp,s,o,a,p,VAT_client,VAT_vet,weight) values (?,?,?,?,?,?,?,?,?,?);";
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $animalName, PDO::PARAM_STR);
$stmt->bindParam(2, $ownerVAT, PDO::PARAM_INT);
$stmt->bindParam(3, $date, PDO::PARAM_STR);
$stmt->bindParam(4, $s, PDO::PARAM_STR);
$stmt->bindParam(5, $o, PDO::PARAM_STR);
$stmt->bindParam(6, $a, PDO::PARAM_STR);
$stmt->bindParam(7, $p, PDO::PARAM_STR);
$stmt->bindParam(8, $clientVAT, PDO::PARAM_INT);
$stmt->bindParam(9, $vetVAT, PDO::PARAM_INT);
$stmt->bindParam(10, $_POST["weight"], PDO::PARAM_STR);
$stmt->execute();
echo('Consult registered in the database successfully!');
echo('<br>');
}catch(PDOException $e){
echo('ERROR Consult was not registered. Execute() failed: ' . $e->getMessage());
echo('<br>');
}
// INSERT CONSULT DIAGNOSIS AFTER
if(isset($_POST["DiagnosticCodes"]) && !empty($_POST["DiagnosticCodes"])){
$diagnosis_str = $_POST["DiagnosticCodes"];
$diagnosis_array = explode(";",$diagnosis_str);
// Begin transaction
$conn->beginTransaction();
$norollback = TRUE;
foreach ($diagnosis_array as $diagnosis){
//Insert each diagnosis into consult_diagnosis
try{
$sql = "INSERT INTO consult_diagnosis (code, name, VAT_owner, date_timestamp) values (?,?,?,?);";
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $diagnosis, PDO::PARAM_INT);
$stmt->bindParam(2, $animalName, PDO::PARAM_STR);
$stmt->bindParam(3, $ownerVAT, PDO::PARAM_INT);
$stmt->bindParam(4, $date, PDO::PARAM_STR);
$stmt->execute();
}catch(PDOException $e){
echo("ERROR Consult diagnosis was not registered." . $e->getMessage());
echo('<br>');
$conn->rollBack();
$norollback = FALSE;
}
}
if($norollback === TRUE){
$conn->commit();
echo('Consult diagnosis "'.$diagnosis_str.'" registered in the database successfully!');
echo('<br>');
}
}
//header("Location: getAnimal.php");
echo "<a href='index.php'> <button> Go back to homepage </button></a><br>";
}