-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.php
66 lines (52 loc) · 1.44 KB
/
helpers.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
<?php
function getDatabaseConnection() {
$db = new PDO('sqlite:database.sqlite');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
}
// in helpers.php
function clearForm() {
header("Location: " . $_SERVER['PHP_SELF']);
exit;
}
function initializeDatabase($database) {
$database->exec("CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER
)");
}
function validateUser($name, $age) {
$errors = [];
if ( empty($name) ) {
$errors['first_name'] = "Please add a name before we can save this record";
}
if (!is_numeric($age) || $age < 0) {
$errors['age'] = "Please enter a valid age.";
}
return $errors;
}
function showError($errors, $name) {
if ( isset($errors[$name]) ) {
$message = $errors[$name];
return "<p class='error'>$message</p>";
}
return "";
}
function createUser($db, $name, $age) {
$errors = validateUser($name, $age);
if ( !empty($errors) ) {
return $errors;
}
try {
$db->exec("INSERT INTO users (name, age) VALUES ('$name', $age)");
return []; // no errors means success, right?
} catch (Exception $error) {
return ['database' => "Database error: " . $error->getMessage()];
}
}
function postForm($name) {
return $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST[$name]);
// there are other request types, so - it's nice
// to make sure you aren't just assuming what it is
}