This repository was archived by the owner on Sep 17, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmodel.php
More file actions
142 lines (137 loc) · 5.46 KB
/
Copy pathmodel.php
File metadata and controls
142 lines (137 loc) · 5.46 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
require_once "pdo.php";
#
# model code for create reading. Update SQL and
# then view item with GET and the new item ID
#
function reading_create_model() {
#step 1: get pdo
$pdo = db_connect();
#step 2: set SQL string
$sql_string = "INSERT INTO
chemicals_reading
(date, time, chlorine_ppm, ph, alkalinity_ppm, cyanuric_acid_ppm)
VALUES
(:date, :time, :chlorine_ppm, :ph, :alkalinity_ppm, :cyanuric_acid_ppm)
";
#step 3: initialize prepared statement
$stmt = $pdo->prepare($sql_string);
#step 4: execute prepared statement with non-bound variables, sql injection protection provided
$ph_filler = $_POST['ph'] ?? null;
print_post_get_session();
echo $ph_filler;
$stmt->execute(array(
':date' => $_POST['date'],
':time' => $_POST['time'],
':chlorine_ppm' => $_POST['chlorine_ppm'],
':ph' => $ph_filler,
':alkalinity_ppm' => $_POST['alkalinity_ppm'],
':cyanuric_acid_ppm' => $_POST['cyanuric_acid_ppm']));
#step 4: access new ID
$new_id = $pdo->lastInsertId();
#step 6: setup alert/flash message
$_SESSION['success'] = "New Reading successfully added (id = $new_id).";
#step 7: close/write session or the success message is dropped on redirect
session_write_close();
return $new_id;
}
function reading_read_model($id) {
#step 1: get pdo
$pdo = db_connect();
#step 2: set SQL string
$sql_string = "SELECT
reading_index,
date,
time,
chlorine_ppm,
ph,
alkalinity_ppm,
cyanuric_acid_ppm
FROM
chemicals_reading
WHERE
reading_index = $id
";
#step 3: initialize prepared statement
$stmt = $pdo->prepare($sql_string);
#step 4: execute prepared statement with no variables passed.
$stmt->execute();
#step 5: fetch returns a single row (all we need) as associative array
$reading = $stmt->fetch();
#step 6: iterate over array storing the data as session variables
foreach($reading as $k => $v) {
$_SESSION[$k] = $v;
}
#finis
}
function reading_update_model() {
#step 1: get pdo
$pdo = db_connect();
#step 2: set SQL string
$id = $_POST['reading_index'];
$sql_string = "UPDATE
chemicals_reading
SET
date = :date,
time = :time,
chlorine_ppm = :chlorine_ppm,
ph = :ph,
alkalinity_ppm = :alkalinity_ppm,
cyanuric_acid_ppm = :cyanuric_acid_ppm
WHERE
reading_index = $id
";
#step 3: initialize prepared statement
$stmt = $pdo->prepare($sql_string);
#step 4: execute prepared statement with no variables passed.
$stmt->execute(array(
':date' => $_POST['date'],
':time' => $_POST['time'],
':chlorine_ppm' => $_POST['chlorine_ppm'],
':ph' => $_POST['ph'],
':alkalinity_ppm' => $_POST['alkalinity_ppm'],
':cyanuric_acid_ppm' => $_POST['cyanuric_acid_ppm']));
$_SESSION['success'] = "Reading $id successfully updated.";
#step 7: close/write session or the success message is dropped on redirect
session_write_close();
}
function reading_delete_model() {
#step 1: get pdo
$pdo = db_connect();
#step 2: set SQL string
#print_post_get_session();
$id = $_POST['reading_index'];
$sql_string = " DELETE FROM
chemicals_reading
WHERE
reading_index = $id
";
#step 3: initialize prepared statement
$stmt = $pdo->prepare($sql_string);
#step 4: execute prepared statement with no variables passed.
$stmt->execute();
# flash message
$_SESSION['success'] = "Reading $id successfully deleted.";
#step 7: close/write session or the success message is dropped on redirect
session_write_close();
}
#
# retrieve session value for use in displaying values and then unset.
# null coalesce/html injection protect plus session unset.
#
function get_session_value($k, $default_value) {
#
# echo the retrieved session value or the provided default value
# unset a session variable if used.
#
if (isset($_SESSION[$k])) {
$session_key = $_SESSION[$k];
unset($_SESSION[$k]);
echo htmlentities($session_key);
return;
} else {
echo $default_value;
return;
}
}
?>